[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
return value ignored sometimes when returning from within a loop
From: |
Edward Welbourne |
Subject: |
return value ignored sometimes when returning from within a loop |
Date: |
Mon, 05 May 2003 15:28:33 +0200 |
Configuration Information [Automatically generated, do not change]:
Machine: i386
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS: -DPROGRAM='bash' -DCONF_HOSTTYPE='i386'
-DCONF_OSTYPE='linux-gnu' -DCONF_MACHTYPE='i386-pc-linux-gnu'
-DCONF_VENDOR='pc' -DSHELL -DHAVE_CONFIG_H -I.
-I/home/doko/packages/bash/bash-2.05a
-I/home/doko/packages/bash/bash-2.05a/include
-I/home/doko/packages/bash/bash-2.05a/lib -g -O2
uname output: Linux whorl 2.2.19pre17 #1 Tue Mar 13 22:37:59 EST 2001 i686
unknown
Machine Type: i386-pc-linux-gnu
Bash Version: 2.05a
Patch Level: 0
Release Status: release
Description:
I was parsing cvs log's output and the function I wrote to do
it went strangely wrong. The following silly script mimics
what was going on:
#!/bin/bash
function demo () {
echo -e 'a\nb\nc\n' | while read nom
do if [ -z "$nom" ]
then break
elif [ "$nom" != a ]
then
[ "$nom" = b ] || break
return 0
fi
echo "d'uh"
done
return 1
}
if demo
then echo Yes
else echo No
fi
yes, I'm sorry, it's a bit contorted. However, if I run it, I
see the "d'uh" message once and get the answer No - i.e. demo
returned 1, which I'm quite sure is wrong. Indeed, if I
replace each use of break with return 1, and remove the last
line of the function, I see one "d'uh" and get the answer Yes,
which is the behaviour I expect. Alternatively (reverting to
use of break and a dangling return 1) if I replace the
"echo | while read" line with "for nom in a b c", the script
again says "d'uh" once and answers Yes.
If this is not a bug, it is certainly very strange behaviour.
I've not tried to reduce it to a smaller test-case, but this
can probably be done !
Repeat-By:
Save the above script to a file, run it.
Remove the "return 1" line and place each "break" with "return 1".
Save the result to a second file, run it.
Starting from the original, replace the "echo | while read" line
with "for nom in a b c", save to a third file, run it.
Notice how the output varies.
Fix:
As indicated, I have a work-around; but no fix, sadly.
- return value ignored sometimes when returning from within a loop,
Edward Welbourne <=