Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check the output of "make" and exit bash script if it fails

Tags:

linux

bash

shell

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

like image 647
CheesePita Avatar asked Jun 18 '15 15:06

CheesePita


People also ask

How do you exit a script if command fails?

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.

How do I know if bash command failed?

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.

How do you check exit status in bash?

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.

How do I exit a bash script?

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”.

How to use make's exit status in a bash script?

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!"

How do I bail out of a bash script?

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.

What happens if the user input is empty in a select loop?

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:

How to check if a process is running or not?

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.


1 Answers

Just check the exit code of make:

cmake . || exit 1
make || exit 1
./pcl_visualizer_demo 
like image 68
choroba Avatar answered Oct 02 '22 04:10

choroba