Bash 脚本打印错误并退出或继续

问题描述 投票:0回答:1

我是 bash 和 shell 脚本新手。我想在我的 Makefile 中执行一个命令。但如果命令抛出错误,我希望 Makefile 抛出错误并退出。否则我希望它继续执行下一个命令。

这是我想要实现的目标的伪代码:

some-makefile-command:
    if command fails execution:
         print the command's error message
         exit
    else:
         execute other commands
bash shell makefile terminal
1个回答
0
投票

嗯,这非常简单,假设您的命令返回成功 (0) 和失败(任何其他 8 位值)的适当值:

command || exit 1

#(rest of your commands)

如果您的命令没有返回相关值,那么您必须根据目标以其他方式评估返回状态。通常,如果命令没有返回有意义的值,您通常会想要测试其输出,因此您可以使用如下所示的内容:

IFS= read -r output < <(command)
[[ -n "$output" ]] || exit 1
#(rest of your commands)
© www.soinside.com 2019 - 2024. All rights reserved.