[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: failglob handling
From: |
Chet Ramey |
Subject: |
Re: failglob handling |
Date: |
Fri, 3 Aug 2018 11:33:14 -0400 |
User-agent: |
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 |
On 8/3/18 9:29 AM, Thomas Deutschmann wrote:
> Hi,
>
> please see the following script:
>
>> #!/bin/bash
>> shopt -s failglob
>> if [[ $? -ne 0 ]]; then
>> echo 'shopt failed'
>> exit 1
>> fi
>>
>> # Let's view current options just to be sure
>> shopt -p
>>
>> # The following glob should fail because /foo/bar does not exist ...
>> # Due to 'failglob' option, bash should stop here
>> echo /foo/bar/*
>>
>> # This code should never run if previous glob failed
>> echo Still alive
>
> Is the assumption true that "Still alive" should *never* be printed
> when "/foo/bar" does not exist?
It might be an assumption, but it's not correct. The documentation says
that failglob affects the command:
"If the failglob shell option is set, and no matches are
found, an error message is printed and the command is not executed."
> When you add "set -e" to the beginning, execution will stop immediately
> after the failing glob, i.e. no "Still alive" will be printed.
Because set -e will cause the shell to exit when a command fails.
>
> However, see the following interesting difference between semicolons and
> newlines:
>
>> $ echo "shopt -s failglob; echo /foo/bar/*; echo alive; " | bash
>> bash: line 1: no match: /foo/bar/*
>> $ echo "shopt -s failglob; echo /foo/bar/*; echo alive; " | sed 's:; :\n:g'
>> | bash
>> bash: line 2: no match: /foo/bar/*
>> alive
>> $ echo "set -e; shopt -s failglob; echo /foo/bar/*; echo alive; " | sed 's:;
>> :\n:g' | bash
>> bash: line 3: no match: /foo/bar/*
The failglob option affects the command. The `difference' between a line
and multiple lines is that bash always reads at least one complete line of
input before parsing and executing any of the commands on that line. As
a result:
shopt -s failglob; echo /foo/bar/*; echo alive
is a single compound command, and
shopt -s failglob
echo /foo/bar/*
echo alive
is three simple commands. So failglob causes the current command to fail,
which means the compound command (first case) or the simple command
(second case).
`failglob' has behaved this way since it was introduced in bash-3.0.
--
``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/
signature.asc
Description: OpenPGP digital signature