Exit-on-error option does not 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' -DLOCALEDIR='/usr/share/locale' -DPACKAGE='bash' -DSHELL -DHAVE_CONFIG_H -I. -I. -I./include -I./lib -D_FILE_OFFSET_BITS=64 -O2 -g -pipe -m32 -march=i386 -mtune=pentium4 uname output: Linux hummer.3amok 2.6.12-1.1381_FC3smp #1 SMP Fri Oct 21 04:03:26 EDT 2005 i686 i686 i386 GNU/Linux Machine Type: i386-redhat-linux-gnu Bash Version: 3.0 Patch Level: 14 Release Status: release Description: The attached script (below) sets the 'exit-on-error' option, in order to prevent deletion of an item if the archiving command fails. However, Bash fails to stop the script when an error occurs, and the item is deleted anyway, which is very unfortunate :( The Bash Reference Manual vaguely refers to '&&' when defining the '-e' option: "Exit immediately if a simple command exits with a non-zero status, unless the command that fails is part of an until or while loop, part of an if statement, part of a && or || list..." *Vaguely*, because it does not specify what is means to the "part of a && list." The && list is defined as: "command1 && command2: command2 is executed if, and only if, command1 returns an exit status of zero." Unfortunately, when "&& list" and "set -e" are used together as in the attached script, they effectively cancel each other out, and result in a non-robust script, which doesn't handle errors. The claim: My claim is that in the attached script, the 'false' command should *not* be considered to be a part of the && list. It is a part of function 'archive_item', which is a part of the script. The behavior of 'archive_item' should not change depending on the context, in which it is invoked from. Script: #! /bin/bash set -e archive_item() { set -e false # this is the command that fails to archive the item echo 'Archived.' } delete_item() { true # this is the command that deletes the item echo 'Deleted!' } archive_item && delete_item Expected output: Actual output: Archived. Deleted! ___ Bug-bash mailing list Bug-bash@gnu.org http://lists.gnu.org/mailman/listinfo/bug-bash
Re: Exit-on-error option does not work as expected
[EMAIL PROTECTED] wrote: Script: #! /bin/bash set -e archive_item() { set -e false # this is the command that fails to archive the item echo 'Archived.' } delete_item() { true # this is the command that deletes the item echo 'Deleted!' } archive_item && delete_item Expected output: Actual output: Archived. Deleted! The right way to do this IMO is to forget 'set -e' and do: archive_item() { set -e if false # command that fails to archive the item then echo 'Archived.' else return $? fi } ...which causes 'archive_item' to return non-zero, so that '&& delete_item' is never executed. -- Matthew find / -user your -name base -print0 | xargs -0 chown us:us ___ Bug-bash mailing list Bug-bash@gnu.org http://lists.gnu.org/mailman/listinfo/bug-bash
Re: Exit-on-error option does not work as expected
[EMAIL PROTECTED] writes: > archive_item && delete_item Only simple commands cause the shell to exit on error with -e. But the commands in the function archive_item are part of a && list. `-e' Exit immediately if a simple command (*note Simple Commands::) exits with a non-zero status, unless the command that fails is part of the command list immediately following a `while' or `until' keyword, part of the test in an `if' statement, part of a `&&' or `||' list, or if the command's return status is being inverted using `!'. A trap on `ERR', if set, is executed before the shell exits. Andreas. -- Andreas Schwab, SuSE Labs, [EMAIL PROTECTED] SuSE Linux Products GmbH, Maxfeldstraße 5, 90409 Nürnberg, Germany PGP key fingerprint = 58CA 54C7 6D53 942B 1756 01D3 44D5 214B 8276 4ED5 "And now for something completely different." ___ Bug-bash mailing list Bug-bash@gnu.org http://lists.gnu.org/mailman/listinfo/bug-bash
implicitcd for bash?
In tcsh (and zsh) the "set implicitcd" is extremely useful when you get used to it. It allows on to type just "DIRECTORY_NAME" instead of "cd DIRECTORY_NAME". At first it might seem like a minor difference, but after getting used to it it's quite helpful, especially when you do a lot of cut and paste... It would be nice to have the same option in bash too. It seems that a patch to implement and document this was even submitted some time ago: http://osdir.com/ml/shells.bash.bugs/2004-09/msg00016.html but there was not reply to it. What is the status of this, was it rejected, or it simply fell through the cracks? Thanks --dan ___ Bug-bash mailing list Bug-bash@gnu.org http://lists.gnu.org/mailman/listinfo/bug-bash
Re: Exit-on-error option does not work as expected
[EMAIL PROTECTED] wrote: > My claim is that in the attached script, the 'false' command should > *not* be considered to be a part of the && list. Nevertheless, it is. The current behavior is too well-entrenched to change it, so the most reliable way to get the behavior you want is to skip "-e" and add "&&" between all commands. paul ___ Bug-bash mailing list Bug-bash@gnu.org http://lists.gnu.org/mailman/listinfo/bug-bash
Re: implicitcd for bash?
> In tcsh (and zsh) the "set implicitcd" is extremely useful when you > get used to it. > It allows on to type just "DIRECTORY_NAME" instead of "cd > DIRECTORY_NAME". At first it might seem like a minor difference, but > after getting used to it it's quite helpful, especially when you do a > lot of cut and paste... Something like this will be in the next major release of bash. Chet -- ``The lyf so short, the craft so long to lerne.'' - Chaucer Live Strong. Chet Ramey, ITS, CWRU[EMAIL PROTECTED]http://tiswww.tis.case.edu/~chet/ ___ Bug-bash mailing list Bug-bash@gnu.org http://lists.gnu.org/mailman/listinfo/bug-bash
Re: Exit-on-error option does not work as expected
On 7/17/07, Paul Jarc <[EMAIL PROTECTED]> wrote: Nevertheless, it is. The current behavior is too well-entrenched to change it, so the most reliable way to get the behavior you want is to skip "-e" and add "&&" between all commands. Change the documentation to insert the bracketed sentence: ... is being inverted using `!'. [If the failing command is inside braces, parenthesis, or in a function, the "unless" conditions above apply also to the grouping as a whole.] A trap on `ERR' ... ___ Bug-bash mailing list Bug-bash@gnu.org http://lists.gnu.org/mailman/listinfo/bug-bash