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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30919778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 13:13:34  来源:igfitidea点击:

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

linuxbashshell

提问by CheesePita

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 的 nube;我已经编写了一个 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 stackoverflow. Some of the problems seem to be that the output of make is not a string for example:

这很好用,除非我的代码无法编译它会执行旧的可执行文件并使我一团糟。我希望将 make 的输出放入 if 语句中,该语句仅在 make 成功时运行可执行文件。我已经在 stackoverflow 上的其他帖子中尝试了很多 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

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

回答by choroba

Just check the exit code of make:

只需检查 make 的退出代码:

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

回答by Trencher

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

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

回答by Brian Agnew

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 -eoption e.g.

并适当退出。按照惯例,退出代码 0 表示成功。如果您有多个可执行文件并且您想在任何返回非零退出代码时退出,您可以告诉 bash 使用-e选项执行此操作,例如

  #!/usr/bin/bash -e

Just make sure that the executables (makeand cmake) follow this convention. See herefor more information on exit codes.

只需确保可执行文件 (makecmake) 遵循此约定。有关退出代码的更多信息,请参见此处

回答by Ben Behar

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 命令的“退出状态”是什么。通常,未成功的构建将以非零状态退出。通过 echo-ing '$?' 仔细检查您的情况是否正确。

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

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