Quote:
Originally posted by babag
...also, tried ahh's example above but the second script didn't run.
|
If your second script didn't run, it is because the last command run in your first script failed.
An example:-
If the last command in your first script was
Code:
[ -f /home/me/file.txt ] && echo "file exists"
and the file /home/me/file.txt does not exist, the test will fail and return an exit code of 1. As this is the last command run in your first script the script will return an exit code of 1, thus the second script will not run.
If the file does exist, the test will pass, and the echo command will run. If the echo command is successful it will return an exit code of 0, your first script will exit with an exit code of 0, and the second script will run.
If you are not aware, a successful command returns an exit code of 0, an unsucessful one another number. Simple commands are usually just 0 or 1, but the programmer may designate other numbers to specify why the command was unsuccessful.
If you want a script to exit with a code of 0 whatever happens, simply write
as the last line.
Using the script from
jlliagre as another example, you could write
Code:
#!/bin/bash
subscript1 || exit 1
subscript2 arg1 arg2 || exit 2
# subscript3 || exit 3# disabled
subscript4 || exit 4
exit 0
Now if you run the script from a terminal, you can type
after it is done and it will print the exit code. From this you will know if it was successful, or if not, where it failed.