| 1 | #!/bin/bash |
| 2 | # getopt-simple.sh |
| 3 | # Author: Chris Morgan |
| 4 | # Used in the ABS Guide with permission. |
| 5 | |
| 6 | |
| 7 | getopt_simple() |
| 8 | { |
| 9 | echo "getopt_simple()" |
| 10 | echo "Parameters are '$*'" |
| 11 | until [ -z "$1" ] |
| 12 | do |
| 13 | echo "Processing parameter of: '$1'" |
| 14 | if [ ${1:0:1} = '/' ] |
| 15 | then |
| 16 | tmp=${1:1} # Strip off leading '/' . . . |
| 17 | parameter=${tmp%%=*} # Extract name. |
| 18 | value=${tmp##*=} # Extract value. |
| 19 | echo "Parameter: '$parameter', value: '$value'" |
| 20 | eval $parameter=$value |
| 21 | fi |
| 22 | shift |
| 23 | done |
| 24 | } |
| 25 | |
| 26 | # Pass all options to getopt_simple(). |
| 27 | getopt_simple $* |
| 28 | |
| 29 | echo "test is '$test'" |
| 30 | echo "test2 is '$test2'" |
| 31 | |
| 32 | exit 0 # See also, UseGetOpt.sh, a modified version of this script. |
| 33 | |
| 34 | --- |
| 35 | |
| 36 | sh getopt_example.sh /test=value1 /test2=value2 |
| 37 | |
| 38 | Parameters are '/test=value1 /test2=value2' |
| 39 | Processing parameter of: '/test=value1' |
| 40 | Parameter: 'test', value: 'value1' |
| 41 | Processing parameter of: '/test2=value2' |
| 42 | Parameter: 'test2', value: 'value2' |
| 43 | test is 'value1' |
| 44 | test2 is 'value2' |
| 45 |