[英]Check the output of “make” and exit bash script if it fails
I'm a nube to more than just bash, however; 但是,我不只是bash的一分子。 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.
我希望将make的输出放到仅在make成功时才运行可执行文件的if语句中。 I've tried a great many bash things from other posts here on stackoverflow.
我在stackoverflow上的其他文章中尝试了很多bash事情。 Some of the problems seem to be that the output of make is not a string for example:
有些问题似乎是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
任何帮助
Just check the exit code of make: 只需检查make的退出代码:
cmake . || exit 1
make || exit 1
./pcl_visualizer_demo
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 默认情况下表示:任何错误
You should probably check the exit code of make eg 您可能应该检查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.
按照惯例,退出代码0表示成功。 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 eg 如果您有多个可执行文件,并且希望在有任何可执行文件返回非零退出代码的情况下进行纾困,则可以使用
-e
选项让bash做到这一点,例如
#!/usr/bin/bash -e
Just make sure that the executables ( make
and cmake
) follow this convention. 只需确保可执行文件(
make
和cmake
)遵循此约定即可。 See here for more information on exit codes. 有关退出代码的更多信息,请参见此处 。
You could try to determine what the 'exit status' of your previous bash command was using '$?'. 您可以尝试确定以前的bash命令的“退出状态”使用的是“ $?”。 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 '$?'
通过回显'$?'仔细检查您的情况是否如此。
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."
这是Bash为函数赋予“返回值”的方式。 .
。 .
。 .
。
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.
从命令行中给出脚本的退出状态,即脚本中执行的最后一条命令,按照惯例,成功时为0,错误时为1到255的整数。
Source: http://www.tldp.org/LDP/abs/html/exit-status.html 资料来源: http : //www.tldp.org/LDP/abs/html/exit-status.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.