[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: if source command.sh & set -e issue
From: |
Chet Ramey |
Subject: |
Re: if source command.sh & set -e issue |
Date: |
Fri, 26 Jul 2024 11:18:57 -0400 |
User-agent: |
Mozilla Thunderbird |
On 7/24/24 1:53 PM, Mor Shalev via Bug reports for the GNU Bourne Again
SHell wrote:
*if source command.sh ; then echo passfi*
The man page says, about this scenario:
"The shell does not exit if the command that fails is
part of the command list immediately following a while
or until keyword, part of the test following the if or
elif reserved words, part of any command executed in a
&& or || list except the command following the final &&
or ||, any command in a pipeline but the last, or if the
command's return value is being inverted with !. If a
compound command other than a subshell returns a non-
zero status because a command failed while -e was being
ignored, the shell does not exit."
The `set -e' is ignored because the `source' command is run in the test
portion of the `if' command.
Or, similarly:
source command.sh && echo pass
The set -e would be ignored because the source command is run as a non-
final part of an and-or list.
looking at the following:
morshalev@morsh:~/workspace1/soc/soc> cat script.sh
#!/bin/bash
(source command.sh) && echo pass
This is run in a subshell.
morshalev@morsh:~/workspace1/soc/soc> cat command.sh
#!/bin/bash
set -e
echo calling tool a
echo calling tool b
false
echo calling tool c
echo calling tool d
The man page says:
"If a compound command or shell function executes in a
context where -e is being ignored, none of the commands
executed within the compound command or function body
will be affected by the -e setting, even if -e is set
and a command returns a failure status. If a compound
command or shell function sets -e while executing in a
context where -e is ignored, that setting will not have
any effect until the compound command or the command
containing the function call completes."
So `set -e' doesn't take effect until the setting is no longer being
ignored. The contents of the sourced file are parsed as a compound
command.
The POSIX text is substantially similar, from
https://pubs.opengroup.org/onlinepubs/9799919799/utilities/V3_chap02.html#tag_19_26
*in 4.2 we get:*morshalev@morshalev:~/workspace16/soc2> ./script.sh
calling tool a
calling tool b
*in 4.4 we get:*
morshalev@morsh:~/workspace1/soc/soc> ./script.sh
calling tool a
calling tool b
calling tool c
calling tool d
pass
It changed in bash-4.3 as the result of
https://www.austingroupbugs.net/view.php?id=52
which codified the current requirements and
https://lists.gnu.org/archive/html/bug-bash/2012-12/msg00012.html
which specifically addressed this case.
There was extensive discussion on the posix shell mailing list in 2009
where we all hashed out the requirements and new behavior.
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU chet@case.edu http://tiswww.cwru.edu/~chet/
OpenPGP_signature.asc
Description: OpenPGP digital signature