[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
strange behaviour of LINENO if function calls are nested
From: |
l . g . e |
Subject: |
strange behaviour of LINENO if function calls are nested |
Date: |
Wed, 10 Jul 2002 18:05:33 +0200 (CEST) |
Configuration Information [Automatically generated, do not change]:
Machine: i386
OS: linux
Compiler: gcc -I/usr/src/packages/BUILD/bash-2.05
Compilation CFLAGS: -DPROGRAM='bash' -DCONF_HOSTTYPE='i386'
-DCONF_OSTYPE='linux' -DCONF_MACHTYPE='i386-suse-linux' -DCONF_VENDOR='suse'
-DSHELL -DHAVE_CONFIG_H -D_FILE_OFFSET_BITS=64 -I. -I/usr/include -I.
-I./include -I./lib -I/usr/include -O2 -mcpu=i486 -march=i486 -D_GNU_SOURCE
-Wall -pipe
uname output: Linux johann 2.4.18-SuSE-16-PKTCDVD #2 Mit Jun 12 16:09:34 CEST
2002 i586 unknown
Machine Type: i386-suse-linux
Bash Version: 2.05
Patch Level: 0
Release Status: release
Description:
behaviour of $LINENO is not usefull/consistent.
---cut---
#!/bin/bash
function try1 {
echo $FUNCNAME $LINENO
echo $FUNCNAME $LINENO
}
function try2
{
echo $FUNCNAME $LINENO
try1
echo $FUNCNAME $LINENO
try3
echo $FUNCNAME $LINENO
echo $FUNCNAME $LINENO
}
function try3 {
echo $FUNCNAME $LINENO
echo $FUNCNAME $LINENO
}
try2
---cut---
output is
try2 1
try1 1
try1 2
try2 8
try3 1
try3 2
try2 -3
try2 -2
output should be either
(LINENO relativ to current function, restored on exit of sub function)
try2 1
try1 1
try1 2
try2 3
try3 1
try3 2
try2 5
try2 6
or
(LINENO absolute wrt file)
try2 8
try1 3
try1 4
try2 10
try3 16
try3 17
try2 12
try2 13
Repeat-By:
See above
Fix:
no real fix, but a workaround for the purpose I use this feature
I come from perl, and I like the "do or die" concept.
---cut---
#!/bin/bash
shopt -s expand_aliases
alias die='reset_lineno; die_at "$0" "$FUNCNAME" "$LINENO"'
MAGIC_LINENO=$(( LINENO + 2 ))
function reset_lineno
{
# "this is a dirty hack!"
return 0
}
function die_at
{
local FILE=$1 FUNC=$2 LINE=$3
shift 3
[[ $* ]] || set -- "Died."
echo -n >&2 "$*"
# if we are in a function, append it to FILE
# otherwise set MAGIC_LINENO to zero, since we are on the
script level
# and LINENO is valid.
[[ $FUNC ]] && FILE="$FILE ($FUNC)" || MAGIC_LINENO=0
[[ "$*" != *$'\n' ]] \
&& echo -e "\n stopped at $FILE line $(( LINE + MAGIC_LINENO
))."
exit 255
}
function try1 {
echo try1 $LINENO
try2
echo try1 $LINENO
[[ assert == true ]] || { die "just to show how it works."; }
}
function try2 {
return 0
}
try1
---cut---
output is
try1 1
try1 -3
just to show how it works.
stopped at lineno_bug.demo (try1) line 28.
I consider the last line real usefull debugging aid, whereas the
atempt to use a flow trace like
"echo in $FUNCNAME $LINENO: doing this and that"
is misleading and confusing at best.
Thanks,
Lars
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- strange behaviour of LINENO if function calls are nested,
l . g . e <=