假设我有以下(毫无意义的)Bash 函数:
myfunc() {
ls
failfailfail
uptime
}
Run Code Online (Sandbox Code Playgroud)
我按如下方式运行它:
myfunc || echo "Something is wrong."
Run Code Online (Sandbox Code Playgroud)
我想要发生的是ls运行(因为它应该),failfailfail不起作用(因为它不存在),并且uptime不运行。该函数的返回值将是非零的,并且将显示错误消息。返回值不必是失败命令的确切退出状态,它不应该为零。
而实际上,我得到的输出ls,其次是“-bash:failfailfail:未找到命令”,然后输出uptime。错误消息未显示,因为失败的退出状态正在被吃掉。
set -e无论是在函数内还是在调用函数的作用域内,都没有任何有用的效果。我可以让它按照我想要的方式工作的唯一方法是:
myfunc() {
ls || return $?
failfailfail || return $?
uptime || return $?
}
Run Code Online (Sandbox Code Playgroud)
但这似乎非常重复和丑陋。有没有另一种方法来清理它?
我有两个命令,build和deploy. 目前我build手动运行,用我自己的眼睛解析它的输出,并使用我在输出中找到的值作为deploy. 该过程类似于:
$ build
==> amazon-ebs: amazon-ebs output will be in this color.
... hundreds of lines of output ...
==> Builds finished. The artifacts of successful builds are:
--> amazon-ebs: AMIs were created:
us-east-1: ami-19601070
$ deploy ami-19601070
... some more output ...
Run Code Online (Sandbox Code Playgroud)
(build实际上是Packer,为了精明)
我想将这两个步骤结合在一个脚本中。粗略的大纲将包括以下内容:
build0并且输出包含字符串“AMIs was created”,否则中止ami-19601070从输出中提取 AMI 编号 ( )deploy ami-19601070我试图想出将所有内容连接在一起的最佳方法,最好是使用 shell 脚本,但我一直坚持如何为两个单独的模式 grep 输出,同时理想情况下,仍然将所有 …