Advertisement
MertcanGokgoz

Bash Try And Catch

Dec 30th, 2018
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.31 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. function try()
  4. {
  5.     [[ $- = *e* ]]; SAVED_OPT_E=$?
  6.     set +e
  7. }
  8.  
  9. function throw()
  10. {
  11.     exit $1
  12. }
  13.  
  14. function catch()
  15. {
  16.     export ex_code=$?
  17.     (( $SAVED_OPT_E )) && set +e
  18.     return $ex_code
  19. }
  20.  
  21. function throwErrors()
  22. {
  23.     set -e
  24. }
  25.  
  26. function ignoreErrors()
  27. {
  28.     set +e
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement