Bash Case Issues..

Hi,

I'm having some trouble with using "case...esac" in Bash.

I've googled it and am stuggling to understand the syntax and how to do certain things.

Firstly, I want to be able to choose a case based on a variable number

For example, I have in my code a place where a user can enter a number using read.

read SEL

Then I want to use a case to decide what should be done.

I want it to be able to figure out whether it is a number or letter.

Using "if" you can always use

if [ $SEL -eq $SEL 2>/dev/null ]
then
    echo "Is Number"
else
    echo "Contains Letters or Symbols"
fi

I also want to have a case based on ranges of numbers too, but the upper range isn't a fixed number

So, in my code I find what the last number will be and define it as LASTNUM

In my case I want it be

case "$SEL"
    0)
        # Do Something for 0
    ;;
    1-$LASTNUM)
        # Do something for between 1 and LASTNUM
    ;;
    Any variation of Q q or Quit)
       # Do something for Q, q or Quit
    ;;
    *)
       # Do something for everything else
    ;;
esac

I've been really struggling and any help would be much appreciated.

Thanks in advance! :smiley:

case "$SEL" in
    0)
       echo zero
    ;;
    [1-9]*)
       echo number
    ;;
    [qQ]|[qQ]uit)
       echo quit
    ;;
    *)
       echo no choice
    ;;
esac
1 Like

Thanks that works perfectly!