I am trying to write a function that logs execution of the *next* line.
The usage would be:
#/bin/bash
log=/tmp/mylog.log
var1="some variable"
log
echo "Unfortunately this line gets executed twice" | tee -a
/tmp/temp/bla-bla.tmp
The problem is that I can't reliably get to modify `BASH_LINENO[0]`. I
swear, that I used to have success in it, but now everytime I change its
value (both inside or outside the function body) the assignment gets
ignored. Is there any way to get the bash to skip execution of the next
line?
The script goes along these lines:
function log()
{
case $- in *x*) USE_X="-x";; *) USE_X=;; esac
set +x
if [ -n "$log" ]; then
file=${BASH_SOURCE[1]##*/}
linenr=$((${BASH_LINENO[0]} + 1 ))
line=`sed "1,$((${linenr}-1)) d;${linenr} s/^ *//; q" $file`
if [ -f /tmp/tmp.txt ]; then
rm /tmp/tmp.txt
fi
echo "$line" > /tmp/tmp2.txt
BASH_LINENO[0]=$((${BASH_LINENO[0]}+1)) #EXECUTES FINE, BUT
DOESN'T CHANGE THE CALL STACK
mymsg=`msg2`
exec 3>&1 4>&2 >>/tmp/tmp.txt 2>&1
set -x
source /tmp/tmp2.txt
exitstatus=$?
set +x
exec 1>&3 2>&4 4>&- 3>&-
#...
#Here goes part, that parses the /tmp/tmp.txt and appends stuff
into $log
#...
fi
if [ -n "$USE_X" ]; then
set -x
fi
if [ "$exitstatus" -ne "0" ]; then
exit $exitstatus
fi
}
----
This message is a crosspost from
http://stackoverflow.com/questions/27259418/how-to-modify-call-stack-in-bash
Adam