Bash case statement

后端 未结 3 1173
傲寒
傲寒 2020-12-31 11:02

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

I\'m starting off with the below

#!/bin/sh
case $@ in

    -h|--help)
                 


        
3条回答
  •  Happy的楠姐
    2020-12-31 11:39

    Try this

    #!/bin/sh
    
    usage() {
        echo `basename $0`: ERROR: $* 1>&2
        echo usage: `basename $0` '[-a] [-b] [-c] 
            [file ...]' 1>&2
        exit 1
    }
    
    
    while :
    do
        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;;
        esac
        shift
    done
    

提交回复
热议问题