code_junkie

Exit Shell Script Based on Process Exit Code

Nov 10th, 2011
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. ls -al file.ext
  2. rc=$?
  3. if [[ $rc != 0 ]] ; then
  4. exit $rc
  5. fi
  6.  
  7. ls -al file.ext | sed 's/^/xx: /"
  8.  
  9. set -e
  10. set -o pipefail
  11.  
  12. command1 && command2 && command3
  13.  
  14. command1 && command2 && command3
  15.  
  16. if command1
  17. then
  18. if command2
  19. then
  20. do_something
  21. else
  22. exit
  23. fi
  24. else
  25. exit
  26. fi
  27.  
  28. # this will trap any errors or commands with non-zero exit status
  29. # by calling function catch_errors()
  30. trap catch_errors ERR;
  31.  
  32. #
  33. # ... the rest of the script goes here
  34. #
  35.  
  36. function catch_errors() {
  37. # do whatever on errors
  38. #
  39. #
  40. echo "script aborted, because of errors";
  41. exit 0;
  42. }
  43.  
  44. [ $? -eq 0 ] || exit($?) # exit for none-zero return code
Add Comment
Please, Sign In to add comment