]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - misc-utils/getopt-parse.bash
3 # A small example script for using the getopt(1) program.
4 # This script will only work with bash(1).
5 # A similar script using the tcsh(1) language can be found
6 # as getopt-parse.tcsh.
8 # Example input and output (from the bash prompt):
10 # ./getopt-parse.bash -a par1 'another arg' --c-long 'wow!*\?' -cmore -b " very long "
12 # Option c, no argument
13 # Option c, argument 'more'
14 # Option b, argument ' very long '
15 # Remaining arguments:
20 # Note that we use "$@" to let each command-line parameter expand to a
21 # separate word. The quotes around "$@" are essential!
22 # We need TEMP as the 'eval set --' would nuke the return value of getopt.
23 TEMP
=$
(getopt
-o 'ab:c::' --long 'a-long,b-long:,c-long::' -n 'example.bash' -- "$@")
26 echo 'Terminating...' >&2
30 # Note the quotes around "$TEMP": they are essential!
42 echo "Option b, argument '$2'"
47 # c has an optional argument. As we are in quoted mode,
48 # an empty parameter will be generated if its optional
49 # argument is not found.
52 echo 'Option c, no argument'
55 echo "Option c, argument '$2'"
66 echo 'Internal error!' >&2
72 echo 'Remaining arguments:'