nabmaster nabmaster - 10 months ago 32
Bash Question

How can I delimit a variable to be within a range of numbers in bash?

read primeranota
if test $primeranota ={1..10} -o $primeranota!={A..Z} -o $primeranota !={a..z} ; then
echo "Nota introducida con exito."
until "$primeranota" -ge 0 -a "$primeranota" -le 10; do
echo -n "Escribe una nota del [0-10]: "
read primeranota

I also wanted to limit the input to just numbers, but I dunno how can I limit that.

I ve been stuck with this for days.. Thanks in advance.


You can use a pattern:

unset input
shopt -s extglob # For bash < 4.1.
until [[ $input == @(0|[1-9]*([0-9])) && $input -le 10 ]]; do
    read -r input
  • @ means one of the alternatives must be present, i.e. either 0, or a positive number
  • positive number starts with a non-zero [1-9], followed by any digit (or none) *([0-9]).