小编zan*_*etu的帖子

尽管出现错误,如何保持 dash shell 脚本运行?

我有一个破折号 shell 脚本,旨在运行到文件末尾:

#!/usr/bin/dash
# File b_shell
. a_nonexistent_file
echo "I want to print this line despite any previous error."
Run Code Online (Sandbox Code Playgroud)

但是,它在无法获取不存在的文件后停止运行:

$ ./b_shell
./b_shell: 3: .: a_nonexistent_file: not found
Run Code Online (Sandbox Code Playgroud)

我尝试过包括set -e和在内的候选解决方案 || true,这些解决方案可以在互联网上轻松找到,但无济于事。

附加说明:

  1. 我不被允许使用其他 shell,例如 Bash、tcsh 等。请不要让我切换。
  2. 我注意到其他一些“未找到”错误不会停止脚本,如下所示。
#!/usr/bin/dash
# File b_shell_ok
a_nonexistent_command
ls a_nonexistent_file
echo "This line is printed despite any previous error."
Run Code Online (Sandbox Code Playgroud)
$ ./b_shell_ok
./b_shell_ok: 3: a_nonexistent_command: not found
ls: cannot access 'a_nonexistent_file': No such file or directory
This line is printed despite any …
Run Code Online (Sandbox Code Playgroud)

shell dash shell-script

7
推荐指数
2
解决办法
1392
查看次数

标签 统计

dash ×1

shell ×1

shell-script ×1