I'm working with the Perforce command line client. I want to check for the case when a user tries to unshelve an unsubmitted changelist that has a valid changelist number, but has no shelved files. In this case the p4 unshelve
command will still have an exit status of 0, but it will print an error message to stderr.
I'm fairly inexperienced with bash scripts, but I did run my code through shellcheck.net to find any obvious errors.
In a lot of places I think this script could be more concise or robust, but I don't know exactly what I should be changing.
Any help is greatly appreciated.
#!/bin/bash
p4 unshelve -f -s "$1" &> tmp_file
# capture the exit status of the call to p4 unshelve
if [ $? -eq 0 ]
then
flag0=0
else
flag0=1
fi
std_err_output="$(cat tmp_file)"
error_message="No such file(s)."
# even if the exit status is 0, still check that the error message was not thrown.
if [ "$std_err_output" = "$error_message" ]
then
flag1=1
else
flag1=0
fi
rm tmp_file
exit_status="expr $flag0 | $flag1"
exit "$($exit_status)"