0

In a bash script below how do you get and store the return code when you run the python script that has arguments? Am I doing it correctly?

When I run the python script on its own it returns the correct value but when I run it on the script it doesn't print the correct value?

#!/bin/bash
BLOB="cpp.blob"
run_on_hardware_result=$(python3 mcmRunHW.py --blob $BLOB) 
echo "run on hardware result is" 
echo "$run_on_hardware_result"
exit  
cherry aldi
  • 327
  • 1
  • 10
  • 1
    Your code already *does* retrieve it; that's what `$?` refers to. Could you be more specific about what's broken/wrong/missing? If you just want to know how to copy one shell parameter to another shell variable, that's pretty straightforward, and already covered in other Q&A. – Charles Duffy Sep 30 '18 at 14:04
  • @CharlesDuffy thank you I've updated my question. – cherry aldi Sep 30 '18 at 14:37
  • So, the first version was working with `$?`, the exit status; the new version, by using `$(...)`, is capturing stdout. Is your question how to preserve exit status while manipulating stdout? In that case, you may want `mcmRun_stdout=$(python3 mcmRunHW --blob "$BLOB"); mcmRun_retval=$?; printf 'run on hardware result is:\n%s\n' "$mcmRun_stdout"; exit "$mcmRun_retval"` -- but I had to guess at whether that's what you're asking for, as the question is still rather unclear. – Charles Duffy Sep 30 '18 at 18:23
  • (As an aside, consider lowercase names for your own variables; all-caps names are used for variables meaningful to the shell itself, whereas names with at least one lower-case character are reserved for application use and guaranteed not to conflict with behavior of POSIX-standardized utilities; see http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html) – Charles Duffy Sep 30 '18 at 18:25

0 Answers0