-1

Edit the content of mytest.sh.

#!/bin/bash    
ARGS=`getopt -o c`
eval set -- "${ARGS}"

while true;
do
    case "$1" in
        -c)
            echo "i am c"
            ;;
        *)
            echo "Internal error!"
            exit 1
            ;;
    esac
done

bash mytest.sh -c get the error info Internal error! ,why can't trigger the info i am c?

JJJ
  • 32,902
  • 20
  • 89
  • 102
  • 1
    Have you tried `echo "$1"` to see what's in $1? (And then perhaps `echo "$2"` and so on...) – JJJ Jun 17 '18 at 09:28
  • 1
    This might help: [How to debug a bash script?](http://unix.stackexchange.com/q/155551/74329) – Cyrus Jun 17 '18 at 09:59
  • See: [An example of how to use getopts in bash](https://stackoverflow.com/q/16483119/3776858) – Cyrus Jun 17 '18 at 10:56
  • May be you are using different implementation of getopt. I know that at-least there are BSD version and GNU version. – apatniv Jun 17 '18 at 16:24

1 Answers1

1
#!/bin/bash    
ARGS=$(getopt -o c -- "$@")
eval set -- "${ARGS}"

while true;
do
    case "$1" in
        -c)
            echo "i am c"
            ;;
        *)
            echo "Internal error!"
            exit 1
            ;;
    esac
done

I really don't know what you intend to do. But the current code you have results in an infinite loop. Better thing would be: break from the while condition after the option is encountered.

Mostly likely a better thing to do:

#!/bin/bash    
ARGS=$(getopt -o c -- "$@")
eval set -- "${ARGS}"

while true;
do
    case "$1" in
        -c)
            echo "i am c"
            # do some processing.
            break
            ;;
        *)
            echo "Internal error!"
            exit 1
            ;;
    esac
done

Note: btw, prefer to use getopts instead of getopt

apatniv
  • 1,771
  • 10
  • 13