weird behaviour of ((count++)) when using , , to change to lower case
Configuration Information [Automatically generated, do not change]: Machine: x86_64 OS: linux-gnu Compiler: gcc Compilation CFLAGS: -DPROGRAM='bash' -DCONF_HOSTTYPE='x86_64' -DCONF_OSTYPE='linux-gnu' -DCONF_MACHTYPE='x86_64-unknown-linux-gnu' -DCONF_VENDOR='unknown' -DLOCALEDIR='/usr/share/locale' -DPACKAGE='bash' -DSHELL -DHAVE_CONFIG_H -I. -I. -I./include -I./lib -g -O2 uname output: Linux eccles 2.6.35-rc6 #1 SMP Fri Jul 23 11:52:29 BST 2010 x86_64 x86_64 x86_64 GNU/Linux Machine Type: x86_64-unknown-linux-gnu Bash Version: 4.1 Patch Level: 2 Release Status: release Description: Incrementing a variable with ((count++)), which should access the value in the variable and then increment it by 1 has some strange behaviour. In some situations it seems to increment the variable before accessing it, and in others it increments it by 2 Repeat-By: Make an array to work with: andy:~$ days=({Mon,Tues,Wednes,Thurs,Fri,Satur,Sun}day) andy:~$ echo ${da...@]} Monday Tuesday Wednesday Thursday Friday Saturday Sunday So far, so good. Now try to access the array by stepping through it with ((count++)) andy:~$ count=0 andy:~$ echo "${days[$((count++))]}, ${days[$((count++))]}, ${days[$((count++))]}" Monday, Tuesday, Wednesday Also good. Now try converting it to lower case with ,, andy:~$ count=0 andy:~$ echo "${days[${count}],,}, ${days[$((count++))],,}, ${days[$((count++))],,}" monday, tuesday, thursday What happened to wednesday? Andy
Re: inconsistent exit status of ((count++))
On 30/07/10 19:55, Stefano Lattarini wrote: At Thursday 29 July 2010, Andrew Benton wrote: andy:~$ count=0 andy:~$ ((count++)) andy:~$ echo $? 1 andy:~$ ((count++)) andy:~$ echo $? 0 I don't think it's a bug, it's just an effect of: 1. `((EXPR))' returning a non-zero exit status iff EXPR evaluates to zero, and That makes no sense to me. Why would evaluating an expression have a non-zero exit status if there was no error? That makes the exit status no use at all for evaluating whether an error has occurred. How can I check for errors if I can't rely on the exit status? How can that not be a bug? Andy
inconsistent exit status of ((count++))
Configuration Information [Automatically generated, do not change]: Machine: x86_64 OS: linux-gnu Compiler: gcc Compilation CFLAGS: -DPROGRAM='bash' -DCONF_HOSTTYPE='x86_64' -DCONF_OSTYPE='linux-gnu' -DCONF_MACHTYPE='x86_64-unknown-linux-gnu' -DCONF_VENDOR='unknown' -DLOCALEDIR='/usr/share/locale' -DPACKAGE='bash' -DSHELL -DHAVE_CONFIG_H -I. -I. -I./include -I./lib -g -O2 uname output: Linux eccles 2.6.35-rc6 #1 SMP Fri Jul 23 11:52:29 BST 2010 x86_64 x86_64 x86_64 GNU/Linux Machine Type: x86_64-unknown-linux-gnu Bash Version: 4.1 Patch Level: 2 Release Status: release Description: [Detailed description of the problem, suggestion, or complaint.] If I use set -e to make my scripts exit on error, the scripts fail if I use something like: count=0 ((count++)) Repeat-By: andy:~$ count=0 andy:~$ ((count++)) andy:~$ echo $? 1 andy:~$ ((count++)) andy:~$ echo $? 0 Fix: This isn't a fix but I can work around this bug if I use ((++count)) andy:~$ count=0 andy:~$ ((++count)) andy:~$ echo $? 0 andy:~$