Sample Header Ad - 728x90

How do I keep a dash shell script running despite any error?

7 votes
2 answers
1728 views
I have a dash shell script that is intended to run to the end of file:
#!/usr/bin/dash
# File b_shell
. a_nonexistent_file
echo "I want to print this line despite any previous error."
However, it stopped running after failure to source a nonexistent file:
$ ./b_shell
./b_shell: 3: .: a_nonexistent_file: not found
I've tried candidate solutions including
-e
and
|| true
, which can be easily found on the Internet, but to no avail. Additional remarks: 1. I am not allowed to use other shells such as Bash, tcsh, etc. Please don't ask me to switch. 2. I noticed some other "Not Found" errors won't stop the script, as is illustrated below.
#!/usr/bin/dash
# File b_shell_ok
a_nonexistent_command
ls a_nonexistent_file
echo "This line is printed despite any previous error."
``` $ ./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 previous error.
Asked by zanetu (171 rep)
Mar 25, 2023, 06:09 AM
Last activity: Mar 25, 2023, 12:09 PM