LDP/LDP/guide/docbook/abs-guide/vartrace.sh

32 lines
853 B
Bash
Raw Normal View History

2001-07-10 14:25:50 +00:00
#!/bin/bash
trap 'echo "VARIABLE-TRACE> \$variable = \"$variable\""' DEBUG
# Echoes the value of $variable after every command.
2012-11-27 14:56:18 +00:00
variable=29; line=$LINENO
2001-07-10 14:25:50 +00:00
2012-11-27 14:56:18 +00:00
echo " Just initialized \$variable to $variable in line number $line."
2001-07-10 14:25:50 +00:00
2012-11-27 14:56:18 +00:00
let "variable *= 3"; line=$LINENO
echo " Just multiplied \$variable by 3 in line number $line."
2001-07-10 14:25:50 +00:00
2014-03-07 02:24:29 +00:00
exit 0
2005-06-05 14:39:50 +00:00
# The "trap 'command1 . . . command2 . . .' DEBUG" construct is
#+ more appropriate in the context of a complex script,
2008-07-20 23:16:47 +00:00
#+ where inserting multiple "echo $variable" statements might be
#+ awkward and time-consuming.
2001-07-10 14:25:50 +00:00
# Thanks, Stephane Chazelas for the pointer.
2005-06-05 14:39:50 +00:00
Output of script:
VARIABLE-TRACE> $variable = ""
VARIABLE-TRACE> $variable = "29"
2008-07-20 23:16:47 +00:00
Just initialized $variable to 29.
2005-06-05 14:39:50 +00:00
VARIABLE-TRACE> $variable = "29"
VARIABLE-TRACE> $variable = "87"
2008-07-20 23:16:47 +00:00
Just multiplied $variable by 3.
2005-06-05 14:39:50 +00:00
VARIABLE-TRACE> $variable = "87"