On 28Aug2018 14:27, Doug H. <fedoraproject.org@xxxxxxxxxxx> wrote:
A "trick" I like to use in any command scripts I write is to have them
tell me how to use them if I run them without any perimeters. Here is
how that works, just need to add the line to play the sound file:
#!/bin/sh
if [ $# -lt 1 ];then
echo
echo "This script will alert you after X seconds."
echo "Use:"
echo 'eggtimer 5'
echo
exit 1
fi
sleep $1
echo Done
Yes, (I assume you mean "parameters"; we have sudo for no "perimeters":-)
Most of my scripts look like this (rough example, details vary a little with
the script):
#!/bin/sh
set -ue
......
cmd=$0
usage="Usage: $cmd blah blah...
more description if warranted .."
badopts=
... parse arguments, set $badopts to 1 if problematic ...
... finally, usually ...
[ $# -gt 0 ] || {
echo "$cmd: missing arguments" >&2
badopts=1
}
...
[ $badopts ] && { echo "$usage" >&2; exit 2; }
... main script here ...
Note:
- "set -ue": abort script on undefined $vars and on _uncaught_ failed command;
this is outstandingly useful for getting your scripts right
- usage message right at the top of the script for easy reference
- error messages to standard error (>&2)
- opportunity to report _every_ argument complaint and set flag for action
later - I loathe commands that complain _and abort_ at the first error; warn
many times, and later abort with usage before the main task begins
- slightly distinct "bad usage" exit code of 2, versus the common value of 1
for generic "operation failed"
- the simple shell script "flag" idiom: empty string false, nonempty string
true, easy "[ $badopts ]" flag test idiom
Cheers,
Cameron Simpson <cs@xxxxxxxxxx>
_______________________________________________
users mailing list -- users@xxxxxxxxxxxxxxxxxxxxxxx
To unsubscribe send an email to users-leave@xxxxxxxxxxxxxxxxxxxxxxx
Fedora Code of Conduct: https://getfedora.org/code-of-conduct.html
List Guidelines: https://fedoraproject.org/wiki/Mailing_list_guidelines
List Archives: https://lists.fedoraproject.org/archives/list/users@xxxxxxxxxxxxxxxxxxxxxxx