Master script calls helper scripts which calls C program, do not know how to get return value of C program
I have a master script that calls a helper script like so:
bash -ex $SOME_DIR/some_tester.sh
The helper script then calls a C program like so:
./some_tester_in_C echo $? . . .other functions called
The issue is that for starters I cannot get the return value from the C program.
The second issue is that nothing after my C program ends gets executed, the "other functions" do not get executed.
One important fact is the C program interacts with the serial ports and is printing things to the terminal. Once it is done talking to the external device it returns a value that I need to capture in my helper script to proceed.
However, if I call the helper script directly from terminal without use of the master script it works fine, I get the return value of the C program and the rest of the code executes.
I tried to reproduce the problem by writing a simple script that calls another which then calls a simple C program and I can reproduce.
I have some
set +x here and there thinking this might be the issue but I do not believe so. I think perhaps the
$? should be something else? But I am not well versed in Linux scripting. Some insight would be helpful.
Since you run the helper script with
bash -e, you will set the
errexit shell option in the script. This shell option will cause the script to terminate if a simple command (like your C command) terminates with a non-zero exit status.
If you want to capture the exit status from your C program, either make sure not to use
-e when starting the helper script or run the program as part of a AND/OR-list (which avoids terminating the script even if
errexit is set and the program exits with a non-zero exit status):
status=0 ./some_tester_in_C || status=$? printf 'Exit status = %sn' "$status"