我有一个破折号 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
,这些解决方案可以在互联网上轻松找到,但无济于事。
附加说明:
#!/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)