There are a few layers here, so bear with me.
My docker-container ssh -c"echo 'YAY!'; exit 25;" command executes echo 'YAY!'; exit 25; in my docker container. It returns:
YAY
error: message=YAY!
, code=25
I need to know if the command within the container was successful, so I append the following to the command:
docker-container ssh -c"echo 'YAY!'; exit 25;" >&1 2>/tmp/stderr; cat /tmp/stderr | grep 'code=' | cut -d'=' -f2 | { read exitStatus; echo $exitStatus; }
This sends the stderr to /tmp/stderr and, with the echo $exitStatus returns:
YAY!
25
So, this is exactly what I want. I want the $exitStatus saved to a variable. My problem is, I am placing this into a bash script (GIT pre-commit) and when this exact code is executed, the exit status is null.
Here is my bash script:
# .git/hooks/pre-commit
if [ -z ${DOCKER_MOUNT+x} ];
then
docker-container ssh -c"echo 'YAY!'; exit 25;" >&1 2>/tmp/stderr; cat /tmp/stderr | grep 'code=' | cut -d'=' -f2 | { read exitStatus; echo $exitStatus; }
exit $exitStatus;
else
echo "Container detected!"
fi;
nullas an exit status; the only possible values are integers. You might have0, but you can't havenull.while readconstruct, insofar as understanding the answer to the other explains why this doesn't work. BashFAQ #24 is pertinent reading as well./tmp/stderris dangerous if you're on a shared system. Two scripts using that same name at once is a problem, but a much worse problem is if another user (or a compromised network service -- and remember,/tmpis world-writable!) replaces/tmp/stderrwith a symlink to a file you have permission to write to but they don't, and which they want to see deleted. Don't use hardcoded temporary filenames -- this is whatmktempexists to avoid.cat foo | grep baris significantly less efficient thangrep bar <foo-- andcat foo | grep bar | cut -d= -f2can be replaced with, say,awk -F= '/bar/ { print $2 }' <footo do everything in one process.