I have a script which measures how long some command executes.
It needs the "real" time
command, meaning, a binary for example in /usr/bin/time
(as the bash-built-in doesn't have the -f
flag).
Below, a simplified script which can be debugged:
#!/bin/bash
TIMESEC=$(echo blah | ( /usr/bin/time -f %e grep blah >/dev/null ) 2>&1 | awk -F. '{print $1}')
echo ABC--$TIMESEC--DEF
if [ "$TIMESEC" -eq 0 ] ; then
echo "we are here!"
fi
Save as "test.sh" and execute:
$ bash test.sh
ABC--0--DEF
we are here!
So it worked.
Now, let's try to debug this by adding "-x" to bash command line:
$ bash -x test.sh
++ echo blah
++ awk -F. '{print $1}'
+ TIMESEC='++ /usr/bin/time -f %e grep blah
0'
+ echo ABC--++ /usr/bin/time -f %e grep blah 0--DEF
ABC--++ /usr/bin/time -f %e grep blah 0--DEF
+ '[' '++ /usr/bin/time -f %e grep blah
0' -eq 0 ']'
test.sh: line 10: [: ++ /usr/bin/time -f %e grep blah
0: integer expression expected
Why does this script break when we're using "-x", and works fine without it?
-x
on, that the$()
construct is getting the-x
output included as part of its resulting value. Don't know if that is "expected" behavior or a bug though... Or maybe it is the subshell()
inside that is actually giving the-x
output.BASH_XTRACEFD
lets you redirectset -x
output to somewhere it's less trouble.