|
AND list "command1 && command2" doesn't work as expected: msg#00169shells.bash.bugs
From: yafrank@xxxxxxx To: bug-bash@xxxxxxx Subject: AND list "command1 && command2" doesn't work as expected Configuration Information [Automatically generated, do not change]: Machine: i386 OS: linux-gnu Compiler: i386-redhat-linux-gcc Compilation CFLAGS: -DPROGRAM='bash' -DCONF_HOSTTYPE='i386' -DCONF_OSTYPE='linux-gnu' -DCONF_MACHTYPE='i386-redhat-linux-gnu' -DCONF_VENDOR='redhat' -DSHELL -DHAVE_CONFIG_H -I. -I. -I./include -I./lib -D_FILE_OFFSET_BITS=64 -O2 -g -pipe -march=i386 -mcpu=i686 uname output: Linux twinhead 2.4.27-lck1-bsdc #1 日 10月 3 16:05:43 CST 2004 i686 i686 i386 GNU/Linux Machine Type: i386-redhat-linux-gnu Bash Version: 2.05b Patch Level: 0 Release Status: release Description: The bash script "Example 26-14. Emulating a push-down stack" from "http://www.tldp.org/LDP/abs/html/arrays.html" works fine and there is an excercise to modify the code to support multiple element push and pop. The homework I did works fine until I took for granted the logical equivalence AND list should give the same result. Detail follows: ********************************************************* #!/bin/bash BP=100 SP=$BP LOW_LIMIT=0 DATA= declare -a stack push () { for (( i=1; i<=$#; i++)); do if [ "$SP" -gt "$LOW_LIMIT" ]; then stack[SP--]="${!i}" else echo "Stack full" break fi done } pop () { count=${1:-1} DATA="" for ((i=0; i<count; i++)); do if [ "$SP" -lt "$BP" ]; then DATA="$DATA ${stack[++SP]}" else echo "Stack empty" break fi # Alter the above "if...else...fi" clause using its logical equivalent #+ [ "$SP" -lt "$BP" ] && DATA="$DATA ${stack[++SP]}" || \ #+ { echo "Stack empty"; break; } #+ won't give the same result. This is not expected. done } status () { echo "-------------------------------------" echo "REPORT" echo "Stack Pointer = $SP" echo "Just popped \""${DATA# }"\" off the stack." echo "-------------------------------------" } pop # empty stack popup status # If AND list used in pop(), reports "SP=101" here echo push garbage pop status v1=23; v2=skiddo; v3=final echo push $v1 $v2 $v3 status pop status pop 2 status exit 0 ********************************************************* The bash manual says that for "command1 && command2", "command2 is executed if, and only if, command1 returns an exit status of zero." It seems conflict here to the first status report "SP=101", which means SP increased by one even if "[ "$SP" -lt "$BP" ]" returns non zero when pop an empty stack. Is there anything wrong here? Repeat-By: Everytime the "if...else...fi" in pop() replaced by logical equivalence AND/OR list as in the comment. Regards ---Frank Wang ________________________________________________________________________ Yahoo! Messenger - Communicate instantly..."Ping" your friends today! Download Messenger Now http://uk.messenger.yahoo.com/download/index.html |
|
| <Prev in Thread] | Current Thread | [Next in Thread> |
|---|---|---|
| Previous by Date: | Re: <<IFS=: foo=a:b:; cmd $foo>> documented: 00169, Stephane CHAZELAS |
|---|---|
| Next by Date: | Remember, We will fully Train you for free.: 00169, Sanders |
| Previous by Thread: | VIRUS IN MAIL TO jn@xxxxxxxxxxxxxxi: 00169, postmaster |
| Next by Thread: | Re: AND list "command1 && command2" doesn't work as expected: 00169, Chet Ramey |
| Indexes: | [Date] [Thread] [Top] [All Lists] |
| News | FAQ | advertise |