Advertisement
bhaveshverma333

Handling errors in bash scripts

Dec 14th, 2022
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.51 KB | Source Code | 0 0
  1. red=`echo -en "\e[31m"`
  2. normal=`echo -en "\e[0m"`
  3.  
  4. # fatal uses SIGUSR1 to allow clean fatal errors
  5. trap "exit 1" 10
  6. PROC=$$ # this will get the current bash script id
  7.  
  8. function error(){
  9.   echo -e "${red}$@${normal}" >&2
  10. #  exit 1
  11.   kill -10 $PROC
  12. }
  13.  
  14. [[ "$1" ]] || error "Error:\nInput file needed\nExample: $0 'file.txt'"
  15. [[ -f "$1" ]] || error "'${1}' file does not exist."
  16.  
  17. [[ "$2" ]] || (echo "Thank you for your input";error "'$1' exists";echo "Exiting Script")
  18.  
  19. echo "This should never output"
Tags: Linux
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement