aboutsummaryrefslogtreecommitdiffstats
path: root/unix/getopt-simple.sh
blob: f5b06f0c620fbca00033397f6111e56b6d32e302 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/bin/bash
# getopt-simple.sh
# Author: Chris Morgan
# Used in the ABS Guide with permission.
#
# See also, UseGetOpt.sh, a modified version of this script.

### example
# $ sh getopt_example.sh /test=value1 /test2=value2

# Parameters are '/test=value1 /test2=value2'
# Processing parameter of: '/test=value1'
# Parameter: 'test', value: 'value1'
# Processing parameter of: '/test2=value2'
# Parameter: 'test2', value: 'value2'
# test is 'value1'
# test2 is 'value2'
###


getopt_simple()
{
    echo "getopt_simple()"
    echo "Parameters are '$*'"
    until [ -z "$1" ]
    do
      echo "Processing parameter of: '$1'"
      if [ ${1:0:1} = '/' ]
      then
          tmp=${1:1}               # Strip off leading '/' . . .
          parameter=${tmp%%=*}     # Extract name.
          value=${tmp##*=}         # Extract value.
          echo "Parameter: '$parameter', value: '$value'"
          eval $parameter=$value
      fi
      shift
    done
}

# Pass all options to getopt_simple().
getopt_simple $*

echo "test is '$test'"
echo "test2 is '$test2'"

exit 0