I'm a nube to more than just bash, however; I've written a bash script that will execute my cmake, make, and c++ executable.
#! /bin/bash
cmake .
make
./pcl_visualizer_demo <-- This is my executable
This works great except when my code fails to compile it executes the old executable and leaves me with a mess. I was hoping to put the output of make into an if statement that only runs the executable when make is successful. I've tried a great many bash things from other posts here on stackoverflow. Some of the problems seem to be that the output of make is not a string for example:
OUTPUT = make
echo $OUTPUT
gives:
[100%] Built target pcl_visualizer_demo
But fails to work with:
if [`expr match "$OUTPUT" '[100%] -eq 5]; then ./pcl_visulizer_demo; fi
Not to mention I think there may be more than one thing wrong with this line. I also tried:
if [diff <(echo "$OUTPUT") <(echo '[100%] Built target pcl_visualizer_demo' -n]; then ./pcl_visulizer_demo; fi
Again it could be that I'm not implementing it right. Any help
Exit When Any Command Fails This can actually be done with a single line using the set builtin command with the -e option. Putting this at the top of a bash script will cause the script to exit if any commands return a non-zero exit code.
Now, every command run in bash shell returns a value that's stored in the bash variable “$?”. To get the value, run this command. $ echo $? If a command succeeded successfully, the return value will be 0.
To check the exit code we can simply print the $? special variable in bash. This variable will print the exit code of the last run command.
One of the many known methods to exit a bash script while writing is the simple shortcut key, i.e., “Ctrl+X”. While at run time, you can exit the code using “Ctrl+Z”.
To utilize make 's exit status in a case like this, execute make from a script: #!/bin/bash make ( [ $? -eq 0 ] && echo "success!") || echo "failure!"
and exit appropriately. By convention, an exit code of 0 indicates success. If you have multiple executables and you want to bail out if any return a non-zero exit code, you can tell bash to do just that with the -e option e.g. Just make sure that the executables ( make and cmake) follow this convention.
The value of the selected item is stored in the variable REPLY. Otherwise, if the user input is empty, the prompt and the menu list are displayed again. The select loop will continue to run and prompt for user input until the break command is executed. To demonstrate how the select construct works, let’s take a look at the following simple example:
First thing that came to my mind for your problem: ps aux | grep-i abc will show the details of the process if its running. You may match the number of lines or time for which its running and compare with zero or any other manipulation. When you run the above command it will show you atleast one line of output i. e.
Just check the exit code of make:
cmake . || exit 1
make || exit 1
./pcl_visualizer_demo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With