I wish to execute different commands and check the return code afterwards before moving to the next steps in the script. At the same time I also wish to log the output of the executed commands to a file using the tee command. Example:
#set non-existing folder
local_path="~/njn"
log_path_file="test.log"
cmd="ls -l ${local_path} | tee -a ${log_path_file}";
eval ${cmd}
returncode=$?
echo "execution result: ${returncode}" | tee -a ${log_path_file};
if [ ${returncode} -eq 0 ]; then
echo "success" | tee -a ${log_path_file}
else
echo "not success" | tee -a ${log_path_file}
fi
returncode is 0 where it should be > 0
I want the returncode variable to have the actual return of the executed command (in this example, the ls -l command.
I've seen there's a solution using a file to write the output of the command to it and then reading the return code from it (Here), but I'm looking for a more elegant solution.
bash
, there is${PIPESTATUS[0]}
. Get exit status of process that's piped to another. Not sure how it interacts witheval
though. – Weijun Zhou Mar 20 '19 at 10:41