I have a function that will execute a few commands.
function doSomething() {
good or bad command || error "reason"
good or bad command || error "other reason"
good or bad command || error "some other reason"
}
If anyone of the commands fails, the error function will be called
# error exit function
function error() {
echo -e "${RED} $1 ${NC}"
exit
}
whenever the error function is called, it is closing the current terminal.
Requirement:
- the error function should exit from the function stack instead of closing the terminal.
Can anyone help me in achieving my requirement from the following two ways:
- Using exit keyword
- Using the return statement (chaining return from the error() to doSomething())
Edit: I just placed the above two functions in a utils.sh file and sourcing it(source utils.sh) in the current terminal.
returninstead ofexit. Else are you sourcing your function files in the current terminal? Make it a script that sources all the functions, then when that exits, it will exit the sub-shell of the script you launched, and return to the cmd-line, ready to do more work. (If I understand your problem description). Good luck.