Source From Here
Question:
I've been writing some shell script and I would find it useful if there was the ability to halt the execution of said shell script if any of the commands failed. See below shell script for an example:
- test.sh
If I execute this shell script and the result will be:
If we modify the test.sh on doTesting() to touch "Error" and execute it:
Here we don't expect Done3 to be generated while Done doesn't exist. Is there a global setting to make the script exit if one of the commands fails?
How-To
Use the set -e builtin:
Alternatively, you can pass
-e on the command line:
Note.
Question:
I've been writing some shell script and I would find it useful if there was the ability to halt the execution of said shell script if any of the commands failed. See below shell script for an example:
- test.sh
- #!/bin/sh
- #set -e
- doTesting()
- {
- touch "Done"
- }
- chkResult()
- {
- [ -e "Done" ] && touch "Done2"
- }
- rm -f Done*
- rm -f Error*
- echo "Running Testing..."
- doTesting
- chkResult
- touch "Done3"
- echo "Bye"
If we modify the test.sh on doTesting() to touch "Error" and execute it:
Here we don't expect Done3 to be generated while Done doesn't exist. Is there a global setting to make the script exit if one of the commands fails?
How-To
Use the set -e builtin:
- #!/bin/bash
- set -e
- # Any subsequent(*) commands which fail will cause the shell script to exit immediately
Note.
沒有留言:
張貼留言