Check The Output of "Make" and Exit Bash Script If It Fails

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

Just check the exit code of make:

cmake . || exit 1
make || exit 1
./pcl_visualizer_demo

In a Bash script, how can I exit the entire script if a certain condition occurs?

Try this statement:

exit 1

Replace 1 with appropriate error codes. See also Exit Codes With Special Meanings.

Automatic exit from Bash shell script on error

Use the set -e builtin:

#!/bin/bash
set -e
# Any subsequent(*) commands which fail will cause the shell script to exit immediately

Alternatively, you can pass -e on the command line:

bash -e my_script.sh

You can also disable this behavior with set +e.

You may also want to employ all or some of the the -e -u -x and -o pipefail options like so:

set -euxo pipefail

-e exits on error, -u errors on undefined variables, and -o (for option) pipefail exits on command pipe failures. Some gotchas and workarounds are documented well here.

(*) Note:

The shell does not exit if the command that fails is part of the
command list immediately following a while or until keyword,
part of the test following the if or elif reserved words, part
of any command executed in a && or || list except the command
following the final && or ||, any command in a pipeline but
the last, or if the command's return value is being inverted with
!

(from man bash)

bash script - check for specific string in output is failing

$? represents just the integer exit code not the output of your psql command. You need to grab the output of psql command and check in if condition.

You can use:

#!/bin/bash

output=$(psql -U postgres -c "CREATE DATABASE test TEMPLATE template0;" 2>&1)
ret=$?

if [[ $ret -eq 0 ]]; then
echo OK
else
if [[ $output == *'already exists'* ]]; then
echo OK
else
echo FAIL
fi
fi

Make Exits Successfully with Errors In A Test

You just need to do it from within the shell script, just as you'd do it if you wrote a shell script. Something like:

tests: $(TSTE)
err=0; \
for test in $^ ; do \
chmod +x ./$${test} ; \
./$${test} || err=$$?; \
done; \
exit $$err

Why doesn't testing exit status work when output is redirected?

[[ ... ]] without a specified test runs [[ -n ... ]]. In this case, it tests whether the captured output is non-empty. But if you redirect everything to /dev/null, the output is indeed empty!

You don't need to capture the output. which should already return a non-zero status when it cannot find the file to execute.

which pacman &> /dev/null && echo pacman

Evaluation of exit code fails in bash

$? echos 0 as the echo success command executes sucessfully, and therefore $? in echo $? is set to 0.

To echo the exit status from sh mycommand, either swap echo success && echo $? to echo $? && echo success or save the exit code of sh mycommand in a variable before the if-condition (and echo the variable).

How to get the exit status and the output of a $(shell command) before make 4.2?

I just can't understand why you're trying to make use of $(shell ...) at all. This is so overly complex, and so meaningless. The only effect of $(shell ...) is that it expands before any recipe is run. But you never try to make use of that. On the contrary, it looks only natural to move the script directly inside the recipe.

For example, consider this:

unused = """
ifeq (true,false)
"""

# some python stuff
print("Hello world")
exit(1)

unused = """
endif

# here comes make stuff
.ONESHELL:
.PHONY: all
all:
@/usr/bin/python $(lastword $(MAKEFILE_LIST))
ec=$$?
echo "script exitcode is $$ec"
exit $$ec

#"""

Maybe even better solution is simply to make python a custom SHELL for a single rule. Although, in this case it could be tricky to mix python and shell scripts in the same rule (but I don't think it's really essential).



Related Topics



Leave a reply



Submit