On 16Jun2018 10:24, Marek Howard <marekhwd@xxxxxxxxx> wrote:
On Tue, 2018-06-12 at 07:52 +1000, Cameron Simpson wrote:
Personally I like "set -x". I've even got a tiny line script called "set-x"
which goes:
#!/bin/sh
set -x
exec "$@"
and many scripts which do variants on:
trace=
[ -t 2 ] && trace=set-x # at least during development
...
$trace important command here ...
...
Thank you for sharing your practice. It looks handy.
For proper utility scripts I tend to add a -x option to explicitly turn it on,
and -n to set it to "eecho", another tiny script which goes:
#!/bin/sh
echo "$*" >&2
which gets you do-nothing tracing mode, 90% of what a -n needs.
Could you please elaborate more on this? In `help set` I see that -n
switches Bash into "dry run" mode.
All Bourne shells honour the -n "set" option.
But this isn't a shell switch, it is a switch for scripts, putting the script
into "dry run" (or "no action", far more memorable) mode. Same meaning for the
switch though; this is very common for shell commands.
So something like:
doit=1
trace=
##[ -t 2 ] && trace=set-x # for debugging
# parse options (incomplete boilerplate code)
while [ $# -gt 0 ]
do
case $1 in
... other options ...
-x) trace=set-x doit=1 # trace execution
-n) trace=eecho doit= ;; # no action
-q) trace= doit=1 ;; # quiet - silent execution
...
esac
shift
done
...
$trace some significant command here
...
if [ $doit ]
then
... some action ...
fi
So the script uses $trace or $doit depending what the use is. For a lot of
things just setting trace to "set-x" (recites commands and runs them), "eecho"
(recites commands to stderr and does not run them) or the empty string (runs
commands without reciting them) covers things.
But something you need to be implementing "dry run" logic yourself, so $trace
isn't enough; you also want a flag you can test. And for that I use $doit, set
to "1" to do stuff and the empty string to not do stuff. This makes for an easy
to read idiom:
if [ $doit ]
then
... do stuff ...
fi
because the test "[ 1 ]" is true and "[ ]" is false.
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/message/7ADMKZ3CEDL5NRGF5Z2FGIAP7AKNDI2B/