I have a simple bash function whose goal is to run a command passed in as a string, check the return code in $?, and if it returns nonzero, print an error specified in the second argument and exit.
Here’s the function:
function checkcommand()
{
$1
if [ "$?" -ne "0" ]; then
echo "$2"
exit 1
fi
}
The function works great. Here are some examples that work:
checkcommand "git clone /home/git/framework.git $gitreponame" "git clone failed!
"
The problem comes when I want to use my function to validate the success of a commit:
checkcommand "git commit -m\"remove migrations from release-$todaysrelease-pr
ep\"" "Commit for migrations removal from release-$todaysrelease-prep failed!"
I’ve tried (as pictured) baclslash quoting, single quotes, various combos of single and double quotes, etc.
Thanks for any clues!
Try
Use
\before"in your command as you did in your example.