开发者

Arrays and Lists in Bourne Shell

开发者 https://www.devze.com 2023-04-05 01:44 出处:网络
I have the code read input case \"$input\" in \"list\"* ) blah ;; \"display\"* ) blah ;; \"identify\"* ) blah ;;

I have the code

read input
case "$input" in
    "list"* )
        blah
        ;;

    "display"* )
        blah
        ;;

    "identify"* )
        blah
        ;;

    "rules"* )
        perl image.pl $input[1]
        ;;

    "quit" )
        echo "Goodbye!"
        ;;

    * )
        echo -n "Error, invalid command. "
        ;;

esac    

I'm trying to figure out how to pass the value of $input to image.pl without including the string "rules" in the input.

I.e, if a user enters 'rules -h' I want to just pass '-h' to image.pl.

Likewise with my other cases, I would like to specificity test if there have been any other arguments passed along in the input, e.g. for 'quit' I would like to test if a user said 'quit x' and throw a specific error that 'quit' d开发者_开发百科oes not accept any other "arguments".

Thanks.


Assuming you are using Bourne shell as the title specify:

read input
set -- $input
case "$1" in
   list)
      blah
      ;;

    rules)
      perl image.pl "$2"
      ;;
esac


You can use $input variable to initialize an array in bash, here is the code:

read input
declare -a arr=($input)
case "${arr[0]}" in
    "list")
        blah
        ;;
    "display")
        blah
        ;;
    "identify")
        blah
        ;;
    "rules")
        shift
        perl image.pl ${arr[1]}
        ;;
    "quit")
        echo "Goodbye!"
        ;;
    *)
        echo -n "Error, invalid command. "
        ;;
esac 
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号