| 1 | #!/bin/bashscip-shell filesystem . . `/bin/bash`. |
| 2 | # Using getopt |
| 3 | |
| 4 | # Try the following when invoking this script: |
| 5 | # sh ex33a.sh -a |
| 6 | # sh ex33a.sh -abc |
| 7 | # sh ex33a.sh -a -b -c |
| 8 | # sh ex33a.sh -d |
| 9 | # sh ex33a.sh -dXYZ |
| 10 | # sh ex33a.sh -d XYZ |
| 11 | # sh ex33a.sh -abcd |
| 12 | # sh ex33a.sh -abcdZ |
| 13 | # sh ex33a.sh -z |
| 14 | # sh ex33a.sh a |
| 15 | # Explain the results of each of the above. |
| 16 | |
| 17 | E_OPTERR=65 |
| 18 | |
| 19 | if [scip-shell system . . `[`. "$#" -eq 0 ] |
| 20 | then # Script needs at least one command-line argument. |
| 21 | echoscip-shell system . . echo. "Usage $0 -[options a,b,c]" |
| 22 | exit $E_OPTERR |
| 23 | fi |
| 24 | |
| 25 | set -- `getoptscip-shell system . . getopt. "abcd:" "$@"` |
| 26 | # Sets positional parameters to command-line arguments. |
| 27 | # What happens if you use "$*" instead of "$@"? |
| 28 | |
| 29 | while [scip-shell system . . `[`. ! -z "$1" ] |
| 30 | do |
| 31 | case "$1" in |
| 32 | -a) echoscip-shell system . . echo. "Option \"a\"";; |
| 33 | -b) echoscip-shell system . . echo. "Option \"b\"";; |
| 34 | -c) echoscip-shell system . . echo. "Option \"c\"";; |
| 35 | -d) echoscip-shell system . . echo. "Option \"d\" $2";; |
| 36 | *) break;; |
| 37 | esac |
| 38 | |
| 39 | shift |
| 40 | done |
| 41 | |
| 42 | # It is usually better to use the 'getopts' builtin in a script. |
| 43 | # See "ex33.sh." |
| 44 | |
| 45 | exit 0 |