view raw
ubuntu101010101 ubuntu101010101 - 10 months ago 61
Bash Question

Bash case statement

I'm trying to learn case as I was to write a fully functional script.

I'm starting off with the below

case $@ in

echo "You have selected Help"
echo "You have selected B"
echo "You have selected C"
echo "Valid Choices are A,B,C"
exit 1

I want to use two of these options:

./ -h -c

But i get this result
Valid Choices are A,B,C

Please can you help out and let me know what I'm doing wrong?

I want to build a script that will do something if you enter one option but do multiple things if you enter multiple.

Also how would i parse $1 to this script as surley which ever option i enter first (-h) will be $1 ??



Try this


usage() {
    echo `basename $0`: ERROR: $* 1>&2
    echo usage: `basename $0` '[-a] [-b] [-c] 
        [file ...]' 1>&2
    exit 1

while :
    case "$1" in
    -a|-A) echo you picked A;;
    -b|-B) echo you picked B;;
    -c|-C) echo you picked C;;
    -*) usage "bad argument $1";;
    *) break;;