2

I'm relatively new to working with bash. I've inherited this bit of code to run a command via SLURM on an HPC system:

CMD="srun -srunParam1 ... -srunParamN ./scriptToRun.sh -scriptParam1"
exec 5>&1
results=$(eval "${CMD}" | tee - >&5)) 

That all works just fine.

But, I need to capture the exit status of just eval "${CMD}", and don't know how to do it.

Initially, I put exitStatus=$? after the results=... command; but, I believe that's catching the status of assigning a value to the results variable, and not of eval ${CMD}

The script goes on to process the output that is in $results. I don't really understand why the file descriptor has been opened here (or, how to properly use file descriptors). But, I'll save that for further research/a separate question.

EDIT: I commented out the bits with the file descriptor, and observed that the script still works, but $results does not contain the output from running $CMD - it only contains the post-processing of $CMD.

2 Answers 2

4

Bash has PIPESTATUS:

results=$(eval "${CMD}" | tee - >&5; exit ${PIPESTATUS[0]}) 
exitStatus=$?

Note that in the code above we are examining the exit status of a command that was run inside a subshell ($(...)); it would not work to access PIPESTATUS in the parent.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I didn't know this one. A good illustration would be true | true | false | true | false | false; declare -p PIPESTATUS
Just what the doctor ordered. I googled a bit to research PIPESTATUS, and came upon: https://www.shellscript.sh/tips/pipestatus/. @Fravadona, your comment is right in line with the way that site describes PIPESTATUS
0

One way to get status is to save it in a file :

results=$( { eval "${CMD}"; echo $? > /tmp/cmd_status.txt; } | tee - >&5)) 
# Process /tmp/cmd_status.txt here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.