Reputation: 13
I have a question regarding bash functions. I'm trying to create a function called "checkexit" that I can call in other functions to check and see if I need to exit that function or not. I wanted to try and use a function to handle this because I don't want to have to use the same code over and over again.
With that said, it's not working because the return command I'm using only exits the function it's currently in and not the function the function "checkexit" is nested inside of.
I know I could take the easy road out and just add this code in wherever I need to but, I wanted to try and find something a little more elegant.
Any help would be greatly appreciated!
Here's the code I have below for the checkexit function:
function checkexit {
if [ -z "$SITEURL" ] || [[ "$SITEURL" != *".whatever.net" ]]; then
#if exitscript set to yes, exit script
if [[ "$exitscript" = "YES" ]]; then
echo "Skipping $SITEURL"
echo
exitscript=""
return
else
echo "No Site URL provided. skipping!"
echo
return
fi
fi
if [[ "$exitscript" = "YES" ]] && [[ "$nodownload" = "YES" ]]; then
echo "Failed to download file, skipping items."
echo
exitscript=""
return
elif [[ "$exitscript" = "YES" ]] && [[ "$noitems" = "YES" ]]; then
echo "No items to download, skipping items."
echo
exitscript=""
return
elif [[ "$exitscript" = "YES" ]]; then
echo "Exit script triggered. Skipping $SITEURL"
echo
exitscript=""
return
fi
}
Upvotes: 1
Views: 40
Reputation: 15273
In checkexit
, make all of your return
statements return an error exit, such as return 1
.
Then in your calling functions, call it with
checkexit || return
Don't try to manage the caller directly from inside the called function.
Upvotes: 2