CCCPaste

BASH script exit on error

#!/bin/bash
###################################################################### 
#Copyright (C) 2022  Kris Occhipinti
#https://filmsbykris.com

#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.

#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.

#You should have received a copy of the GNU General Public License
#along with this program.  If not, see .
###################################################################### 

#this script has an error function that will display a message
#and exit the script even from sub processes

red=`echo -en "\e[31m"`
normal=`echo -en "\e[0m"`

# fatal uses SIGUSR1 to allow clean fatal errors
trap "exit 1" 10
PROC=$$

function error(){
  echo -e "${red}$@${normal}" >&2
#  exit 1
  kill -10 $PROC
}

[[ "$1" ]] || error "Error:\nInput file needed\nExample: $0 'file.txt'"
[[ -f "$1" ]] || error "'${1}' file does not exist."

[[ "$2" ]] || (echo "Thank you for your input";error "'$1' exists";echo "Exiting Script")

echo "This should never output"