The journey through the **util-linux** from the **a** package set of Slackware
continues. First, a tutorial on `getopt`, an argument parser for Bash and
Tcsh. Here is a demonstrative sample script:
#!/usr/bin/bash
## or you can just use /bin/sh
OPTS=`getopt --options f --long foo --alternative -- "$@"`
eval set -- "$OPTS"
echo "Raw input: $OPTS"
while true ; do
case "$1" in
-f|--foo) echo "Option f has been toggled on" ; shift ;;
--) shift ; break ;;
esac
done
# this outputs anything
# left over after parsing
# valid options
for i in "$@" ; do
echo "$i"
done
You can add more options, and you can add an allowance for arguments. Here is
a slightly more complex version of the script:
#!/usr/bin/bash
OPTS=`getopt --options f,b: --long foo,bar: --alternative -- "$@"`
eval set -- "$OPTS"
echo "$OPTS"
while true ; do
case "$1" in
-f|--foo) echo "Option f has been toggled on" ; shift ;;
-b|--bar) echo "Option b has been set to $2" ; shift 2 ;;
--) shift ; break ;;
esac
done
for i in "$@" ; do
echo "$i"
done
After the coffee break, Klaatu covers `kill`. Because he recorded this episode
on the same night as the previous episode, he does make reference to some
settings from the previous episode (specifically, a hostname setting). That
makes this episode a sequel to the previous one, meaning Klaatu owes you an
extra episode sometime.
Also, `mountpoint`, `mount`, `unmount`, `wdctl` and `watchdog`
[gnu.org/software/libc/manual/html_node/Example-of-Getopt.html](https://www.gnu.org/software/libc/manual/html_node/Example-of-Getopt.html)
A great article about the `eval` and `set` on [Linux Journal](https://www.linuxjournal.com/content/bash-preserving-whitespace-using-set-and-eval)
[Watchdog daemon](http://www.sat.dundee.ac.uk/psc/watchdog/watchdog-testing.html)
[Systemd interface to Watchdog](http://0pointer.de/blog/projects/watchdog.html)
shasum -a256=4f760557e34dcaf6e9c4e2f73c6602b1c054debe584cea890953279d334174a6
view more