Re: [PATCH] main: don't raise exception when executing dotcmd() on a non-existent file

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sat, Nov 24, 2018 at 05:20:21PM +0100, Antonio Ospite wrote:
> When sourcing a file with the dotcmd() builtin, dash raises an exception
> when the input file cannot be opened.

> For instance the following script fails prematurely and does not output
> the message:

>   #!/bin/sh

>   . /non-existent-file
>   echo "Survived!"

> In this case, the behavior of dash is different from other shells (e.g.
> bash, zsh) which are more forgiving and allow execution to carry on even
> if the input file cannot be opened.

POSIX is unambiguous (XCU 2.14 Special Built-In Utilities -> dot) that a
non-interactive shell shall abort when a dot script is not found, and
bash and zsh comply to this when standards compliance is requested (e.g.
by naming the shell "sh" in argv[0] or using "set -o posix" in bash or
"set -o posixbuiltins" in zsh). Most other shells (e.g. mksh, FreeBSD
sh, yash) comply to POSIX unconditionally here, like dash currently
does.

> Fix this by passing the INPUT_NOFILE_OK flag when calling setinputfile()
> in dotcmd().

> As a bonus, this also fixes cases like the following:

>   set -e
>   . /non-existent-file || true
>   echo "Survived! Let's do something else..."

> This idiom is sometimes used in shell script to source a config file
> with default values only to provide fallback values if the default
> values were not available.

The above code is specific to bash and zsh non-POSIX modes, and will not
work in a #!/bin/sh script unless specific steps are taken (such as "set
+o posix" or starting the script with "bash script1" rather than
"./script1").

The simple solution is
  [ ! -f /non-existent-file ] || . /non-existent-file
Time-of-check-time-of-use issues should not be an issue for config
files.

Alternatively, one could try
  command . ./non-existent-file || true
but this may ignore more errors than desired (such as syntax errors in
the sourced file) and does not work correctly in yash 2.30.

-- 
Jilles Tjoelker



[Index of Archives]     [LARTC]     [Bugtraq]     [Yosemite Forum]     [Photo]

  Powered by Linux