检查“make”的输出,如果失败则退出bash脚本

时间:2021-11-27 13:57:58

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.

然而,我不仅仅是一个胆怯,不仅仅是狂欢。我写了一个bash脚本,它将执行我的cmake,make和c ++可执行文件。

#! /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 *. Some of the problems seem to be that the output of make is not a string for example:

这很好用,除非我的代码无法编译它执行旧的可执行文件并让我一团糟。我希望将make的输出放入if语句中,该语句仅在make成功时运行可执行文件。我在*上尝试了很多其他帖子中的bash东西。一些问题似乎是make的输出不是一个字符串,例如:

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

再次,可能是我没有正确实施它。任何帮助

4 个解决方案

#1


18  

Just check the exit code of make:

只需检查make的退出代码:

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

#2


4  

You could use

你可以用

set -e

at the beginning of your shell script

在shell脚本的开头

#!/bin/bash
set -e
cmake .
make
./pcl_visualizer_demo

From 'help set'

来自'帮助集'

-e  Exit immediately if a command exits with a non-zero status.

Which by default means: on any error

默认情况下,这意味着:任何错误

#3


2  

You should probably check the exit code of make e.g.

您应该检查make的退出代码,例如

if [ $? -eq 0 ]
then
  echo "Successfully made"
else
  echo "Could not compile" >&2
fi

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.

并适当退出。按照惯例,退出代码为0表示成功。如果你有多个可执行文件,如果有任何返回非零退出代码你想要拯救,你可以告诉bash使用-e选项做到这一点,例如

  #!/usr/bin/bash -e

Just make sure that the executables (make and cmake) follow this convention. See here for more information on exit codes.

只需确保可执行文件(make和cmake)遵循此约定。有关退出代码的详细信息,请参见此处

#4


0  

You could try to determine what the 'exit status' of your previous bash command was using '$?'. Usually, a build that did not succeed will exit with a non zero status. Double check that this is true in your case by echo-ing '$?'

您可以尝试确定先前bash命令的“退出状态”是使用“$?”。通常,未成功的构建将以非零状态退出。通过回显“$?”来仔细检查这是否属实。

Then use an normal if statement to proceed correctly.

然后使用正常的if语句正确继续。

$? reads the exit status of the last command executed. After a function returns, $? gives the exit status of the last command executed in the function. This is Bash's way of giving functions a "return value." . . .
After a script terminates, a $? from the command-line gives the exit status of the script, that is, the last command executed in the script, which is, by convention, 0 on success or an integer in the range 1 - 255 on error.

$?读取上次执行的命令的退出状态。函数返回后,$?给出函数中执行的最后一个命令的退出状态。这是Bash赋予函数“返回值”的方式。 。 。 。脚本终止后,$?从命令行给出脚本的退出状态,即脚本中执行的最后一个命令,按惯例,成功时为0,或者出错时为1 - 255的整数。

Source: http://www.tldp.org/LDP/abs/html/exit-status.html

#1


18  

Just check the exit code of make:

只需检查make的退出代码:

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

#2


4  

You could use

你可以用

set -e

at the beginning of your shell script

在shell脚本的开头

#!/bin/bash
set -e
cmake .
make
./pcl_visualizer_demo

From 'help set'

来自'帮助集'

-e  Exit immediately if a command exits with a non-zero status.

Which by default means: on any error

默认情况下,这意味着:任何错误

#3


2  

You should probably check the exit code of make e.g.

您应该检查make的退出代码,例如

if [ $? -eq 0 ]
then
  echo "Successfully made"
else
  echo "Could not compile" >&2
fi

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.

并适当退出。按照惯例,退出代码为0表示成功。如果你有多个可执行文件,如果有任何返回非零退出代码你想要拯救,你可以告诉bash使用-e选项做到这一点,例如

  #!/usr/bin/bash -e

Just make sure that the executables (make and cmake) follow this convention. See here for more information on exit codes.

只需确保可执行文件(make和cmake)遵循此约定。有关退出代码的详细信息,请参见此处

#4


0  

You could try to determine what the 'exit status' of your previous bash command was using '$?'. Usually, a build that did not succeed will exit with a non zero status. Double check that this is true in your case by echo-ing '$?'

您可以尝试确定先前bash命令的“退出状态”是使用“$?”。通常,未成功的构建将以非零状态退出。通过回显“$?”来仔细检查这是否属实。

Then use an normal if statement to proceed correctly.

然后使用正常的if语句正确继续。

$? reads the exit status of the last command executed. After a function returns, $? gives the exit status of the last command executed in the function. This is Bash's way of giving functions a "return value." . . .
After a script terminates, a $? from the command-line gives the exit status of the script, that is, the last command executed in the script, which is, by convention, 0 on success or an integer in the range 1 - 255 on error.

$?读取上次执行的命令的退出状态。函数返回后,$?给出函数中执行的最后一个命令的退出状态。这是Bash赋予函数“返回值”的方式。 。 。 。脚本终止后,$?从命令行给出脚本的退出状态,即脚本中执行的最后一个命令,按惯例,成功时为0,或者出错时为1 - 255的整数。

Source: http://www.tldp.org/LDP/abs/html/exit-status.html