开发者

Arrays and Lists in Bourne Shell

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜