RE: Possible bug with BASH V4: The "$!"
Chet, Thanks for the detailed feedback. I double check the documentation (man, changelog) issues, those seems to be local problems with our system (for some strange reason, the man pages were mirrored from RH4 image). One last comment about the "double subshell" case: > ( echo "$!") & > ( echo "$!") & The detail of the "$!" include reference to "current subshell", and seems to conflict with the "Shell Execution Environment", I'm not sure how to resolve those conflicts. However, the implemented behavior can lead to strange bugs, as it is very unexpected. My 2 cents is that the Bash3 behavior or reseting the "$!" in sub shells is more likely to prevent errors, and will still be in compliance with the POSIX text. In the following (not very practical) example: ( DO_somthing ; if [ PROBLEM ] ; then kill $!; fi ; sleep 20) & ... ( DO_somthing ; if [ PROBLEM ] ; then kill $!; fi ; sleep 20) & The user is very unlikely to expect the second subshell to kill the first subshell. In any case, I've modified my scripts to use "${BASHPID:-$!}" to get them to work in BASH4 and BASH3. Again, Many thanks. Yair. -Original Message- From: Chet Ramey [mailto:chet.ra...@case.edu] Sent: Thursday, April 11, 2013 2:03 PM To: Lenga, Yair [ICG-MKTS] Cc: 'chet.ra...@case.edu'; 'Dan Douglas'; 'bug-bash@gnu.org' Subject: Re: Possible bug with BASH V4: The "$!" On 4/11/13 12:15 PM, Lenga, Yair wrote: > Dan, Chet: Many thanks for the info about BASHPID. > > I've checked BASHPID on RH6, and it looks OK. Few comments related to making > the change visible, and POSIX compliance. > > + The 'set' command, does not print the BASHPID, this make it very hard to > find it (unless you read every line in the BASH info file !). >It will be very helpful to include BASHPID there (if set). It does, but BASHPID is one of the special shell variables that springs into existence only when it's referenced. Run the following script; it should display two lines of output: sleep 1& echo $BASHPID set | grep ^BASHP > + The man page list BASH_VERSION, etc., but no indication of BASHPID. It's > probably a good idea to put a note next to '$!' about BASHPID. It's in there, and has been since bash-4.0. > + Could not find any change log on my system (probably a problem with RH). > Not sure if it's documented. That depends on your distribution. There is a summary changelog (CHANGES), a file listing new features (NEWS), and a detailed change log (CWRU/changelog) in the FSF bash distribution. > Also, I have a question about POSIX mode (and POSIX compliance). > The POSIX says '$!' is "most recent background command executed from the > current shell". > > So in : > > ( echo "$!") & > ( echo "$!") & > > According to POSIX, both calls should print "" (nothing). As the background > command does not come from the "current" shell. > > With BASH4, The first call will print "" (nothing), second call will print > the PID of the first call. The bash behavior is correct. The Posix requirement that (...) subshells create a `subshell environment', which is an exact copy of the parent shell's state with a couple of minor exceptions, trumps the text you quoted. http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_12 for the exact reference. Chet -- ``The lyf so short, the craft so long to lerne.'' - Chaucer ``Ars longa, vita brevis'' - Hippocrates Chet Ramey, ITS, CWRUc...@case.eduhttp://cnswww.cns.cwru.edu/~chet/
Bash4: Problem retrieving "$?" when running with "-e"
Good Morning, I've encountered another interesting change in behavior between Bash3 and Bash4. I hope that you can help me: The core question is how to retrieve the status of a command, when running with '-e' For production critical jobs, we run the script in '-e', to ensure that all steps are successful. For cases where we allow the command to fail, because we can implement backup, we add explicit error handing. For example. set -ue CHECK_SPACE (FETCH_NEW_DATA) If [ $? = 11 ] ; then FETCH_BACKUP_DATA fi REMOVE_OLD_DATA COPY_NEW_TO_OLD In Bash3, the script could retrieve the return code for FETCH_NEW_DATA, by placing it into a sub-shell, and then examining the value of "$?". In Bash4, the FETCH_NEW_COMMAND failure cause the parent script to fail. The man page says that '-e' will "exit immediately if a simple command (note Simple Command::) exits with non-zero status unless ...". The "simple commands" definition is a "sequence of words separate by blanks ...". According to this definition, the sequence "( simple command )" Is NOT a simple command, and should NOT trigger the "immediate exit". Can anyone comment on my interpretation. Is there alternative solution that will allow retrieval of the status of single commands when running With the '-e' ? Thanks Yair Lenga
Re: Bash4: Problem retrieving "$?" when running with "-e"
On Fri, Apr 12, 2013 at 11:44:49AM +, Lenga, Yair wrote: > The core question is how to retrieve the status of a command, when running > with '-e' http://mywiki.wooledge.org/BashFAQ/105
Re: Bash4: Problem retrieving "$?" when running with "-e"
Am 12.04.2013 13:44, schrieb Lenga, Yair: > Good Morning, > > I've encountered another interesting change in behavior between Bash3 and > Bash4. I hope that you can help me: > > The core question is how to retrieve the status of a command, when running > with '-e' > > For production critical jobs, we run the script in '-e', to ensure that all > steps are successful. For cases where we allow the command to fail, because > we can implement backup, we add explicit error handing. For example. > > set -ue > CHECK_SPACE > (FETCH_NEW_DATA) > If [ $? = 11 ] ; then > FETCH_BACKUP_DATA > fi > REMOVE_OLD_DATA > COPY_NEW_TO_OLD > > In Bash3, the script could retrieve the return code for FETCH_NEW_DATA, by > placing it into a sub-shell, and then examining the value of "$?". > > In Bash4, the FETCH_NEW_COMMAND failure cause the parent script to fail. > > The man page says that '-e' will "exit immediately if a simple command (note > Simple Command::) exits with non-zero status unless ...". > The "simple commands" definition is a "sequence of words separate by blanks > ...". According to this definition, the sequence "( simple command )" > Is NOT a simple command, and should NOT trigger the "immediate exit". > > Can anyone comment on my interpretation. Is there alternative solution that > will allow retrieval of the status of single commands when running > With the '-e' ? > > Thanks > Yair Lenga > > > > try this approach set -ue CHECK_SPACE RVAUE=0 (FETCH_NEW_DATA) || RVALUE=$? If [ $RVALUE = 11 ] ; then FETCH_BACKUP_DATA fi REMOVE_OLD_DATA COPY_NEW_TO_OLD
Re: Bash4: Problem retrieving "$?" when running with "-e"
On 4/12/13 7:44 AM, Lenga, Yair wrote: > The man page says that '-e' will "exit immediately if a simple command (note > Simple Command::) exits with non-zero status unless ...". > The "simple commands" definition is a "sequence of words separate by blanks > ...". According to this definition, the sequence "( simple command )" > Is NOT a simple command, and should NOT trigger the "immediate exit". > > Can anyone comment on my interpretation. Is there alternative solution that > will allow retrieval of the status of single commands when running > With the '-e' ? You appear to be using bash-4.x and reading the bash-3.x manual page. The bash-4.0 man page says Exit immediately if a \fIpipeline\fP (which may consist of a single \fIsimple command\fP), a \fIsubshell\fP command enclosed in parentheses, or one of the commands executed as part of a command list enclosed by braces (see .SM .B SHELL GRAMMAR above) exits with a non-zero status. The same language is in the man page through bash-4.2. There has been extensive discussion of the changes to -e between bash-3.2 and bash-4.0, which brought bash closer to Posix. Bash wasn't totally Posix-conformant until bash-4.2. See http://lists.gnu.org/archive/html/bug-bash/2012-12/msg00102.html for a summary. Chet -- ``The lyf so short, the craft so long to lerne.'' - Chaucer ``Ars longa, vita brevis'' - Hippocrates Chet Ramey, ITS, CWRUc...@case.eduhttp://cnswww.cns.cwru.edu/~chet/
RE: Bash4: Problem retrieving "$?" when running with "-e"
Chet, Sorry again for pulling the wrong Bash 4 doc. Based on the input, I'm assuming that the portable way (bash 3, bash 4 and POSIX) to retrieve $? When running under "-e" is to use the PIPE CMD_STAT=0 ; GET_MAIN_DATA || CMD_STAT=$? If [ "$CMD_STAT" = 11 ] ; then GET_BACKUP_DATA Fi Any other suggestion for portable code will be appreciated. Yair. -Original Message- From: Chet Ramey [mailto:chet.ra...@case.edu] Sent: Friday, April 12, 2013 11:16 AM To: Lenga, Yair [ICG-MKTS] Cc: 'bug-bash@gnu.org'; chet.ra...@case.edu Subject: Re: Bash4: Problem retrieving "$?" when running with "-e" On 4/12/13 7:44 AM, Lenga, Yair wrote: > The man page says that '-e' will "exit immediately if a simple command (note > Simple Command::) exits with non-zero status unless ...". > The "simple commands" definition is a "sequence of words separate by blanks > ...". According to this definition, the sequence "( simple command )" > Is NOT a simple command, and should NOT trigger the "immediate exit". > > Can anyone comment on my interpretation. Is there alternative solution > that will allow retrieval of the status of single commands when running With > the '-e' ? You appear to be using bash-4.x and reading the bash-3.x manual page. The bash-4.0 man page says Exit immediately if a \fIpipeline\fP (which may consist of a single \fIsimple command\fP), a \fIsubshell\fP command enclosed in parentheses, or one of the commands executed as part of a command list enclosed by braces (see .SM .B SHELL GRAMMAR above) exits with a non-zero status. The same language is in the man page through bash-4.2. There has been extensive discussion of the changes to -e between bash-3.2 and bash-4.0, which brought bash closer to Posix. Bash wasn't totally Posix-conformant until bash-4.2. See http://lists.gnu.org/archive/html/bug-bash/2012-12/msg00102.html for a summary. Chet -- ``The lyf so short, the craft so long to lerne.'' - Chaucer ``Ars longa, vita brevis'' - Hippocrates Chet Ramey, ITS, CWRUc...@case.eduhttp://cnswww.cns.cwru.edu/~chet/
Re: Bash4: Problem retrieving "$?" when running with "-e"
Am 12.04.2013 18:26, schrieb Lenga, Yair: > Chet, > > Sorry again for pulling the wrong Bash 4 doc. > > Based on the input, I'm assuming that the portable way (bash 3, bash 4 and > POSIX) to retrieve $? When running under "-e" is to use the PIPEr > CMD_STAT=0 ; GET_MAIN_DATA || CMD_STAT=$? That isn't a pipe its a logical or, it means if the first command returns non 0 execute the next command. as the assignment will not fail it avoids the problem. as such you could also do GET_MAIN_DATA || GET_BACKUP_DATA or if ! GET_MAIN_DATA ; then GET_BACKUP_DATA fi
trap EXIT in piped subshell not triggered during wait
Hi! I've got strange behavior. Here's my script: #!/bin/bash { trap ' echo "in trap EXIT">&2 ' EXIT sleep 4 & echo 'sleep 2'>&2 sleep 2 echo 'wait $!'>&2 wait $! echo 'exit'>&2 exit } | cat If I press Ctrl-C during wait, the trap isn't triggered. If I replace curly brackets with round brackets, it works. What's the difference?