erical
erical

Reputation: 365

"Program Aborted" but didn't return error code

Today when I checked the log of a script, I found a command(unrar) failed with ("Program Aborted") but no error reported,

this is my script excerpt:

unrar ...|tail -10 >> unrar.log  #I found "Program Aborted" here

if [[ "${?}" -ne "0" ]] 
then 
    echo "[ERROR] unrar application failed with $? errorcode"
else
    echo "[INFO] unrar application succeeded"

Is this due to something wrong with my script or the system itself?

Upvotes: 1

Views: 1275

Answers (1)

jordanm
jordanm

Reputation: 34924

The $? variable contains the exit status of the last command ran. In your case this is the exit status of the "tail" command, which did not fail. In bash, the exit status you are looking for is in the PIPESTATUS array. You can iterate over the array to see if any of the commands in the last pipeline gave a non-zero exist status.

failed=false
for status in "${PIPESTATUS[@]}"; do
    if (( status != 0 )); then
       failed=true
       break
    fi
done

if $failed; then
   echo "[ERROR] unrar application failed with $status errorcode"
else
   echo "[INFO] unrar application succeeded"
fi

Upvotes: 4

Related Questions