我有以下命令
ads2 cls create
这个命令可能会返回两个输出,一个合理的看起来像:
kernel with pid 7148 (port 9011) killed
kernel with pid 9360 (port 9011) killed
probing service daemon @ http://fdt-c-vm-0093.fdtech.intern:9010
starting kernel FDT-C-VM-0093 @ http://fdt-c-yy-0093.ssbt.intern:9011 name=FDT-C-VM-0093 max_consec_timeouts=10 clustermode=Standard hostname=FDT-C-VM-0093 framerate=20000 schedmode=Standard rtaddr=fdt-c-vm-0093.fdtech.ssbt tickrole=Local tickmaster=local max_total_timeouts=1000
kernel FDT-C-VM-0093 running
probing service daemon @ http://172.16.xx.xx:9010
starting kernel FDT-C-AGX-0004 @ http://172.16.xx.xx:9011 name=FDT-C-AGX-0004 max_consec_timeouts=10 clustermode=Standard hostname=FDT-C-AGX-0004 framerate=20000 schedmode=Standard rtaddr=172.16.xx.xx tickrole=Local tickmaster=local max_total_timeouts=1000
kernel Fxx-x-xxx-xxx4 running
>>> start cluster establish ...
>>> cluster established ...
nodes {
node {
name = "FDT-C-VM-xxxx";
address = "http://fxx-x-xx-0093.xxx.intern:xxxx/";
state = "3";
}
node {
name = "xxx-x-xxx-xxx";
address = "http://1xx.16.xx.xx:9011/";
state = "3";
}
}
还有一个不合理的:
kernel with pid 8588 (port 9011) killed
failed to probe service daemon @ http://xxx-c-agx-0002.xxxx.intern:90xx
在这两种方式中,我都将此输出传递给 awk 以检查节点的状态,以防返回合理的输出,否则它应该退出整个脚本 (line 28
)。
ads2 cls create | awk -F [\"] ' BEGIN{code=1} # Set the field delimiter to a double quote
/^>>> cluster established .../ {
strt=1 # If the line starts with ">>> cluster established ...", set a variable strt to 1
}
strt!=1 {
next # If strt is not equal to 1, skip to the next line
}
$1 ~ "name" {
cnt++; # If the first field contains name, increment a cnt variable
nam[cnt]=$2 # Use the cnt variable as the index of an array called nam with the second field the value
}
$1 ~ "state" {
stat[cnt]=$2; # When the first field contains "state", set up another array called stat
print "Node "nam[cnt]" has state "$2 # Print the node name as well as the state
}
END {
if (stat[1]=="3" && stat[2]=="3") {
print "\033[32m" "Success" "\033[37m" # At the end of processing, the array is used to determine whether there is a success of failure.
}
28 else {
29 print "\033[31m" "Failed. Check Nodes in devices.dev file" "\033[37m"
30 exit code
}
}'
some other commands...
请注意,此代码块是 bash 脚本的一部分。
我要做的只是阻止整个脚本(其余的命令)在它进入 line 29
时继续执行,其中 exit 1
代码实际上应该完成这项工作。但是它不起作用。换句话说。它实际上打印语句 Failed. Check Nodes in devices.dev file
。但是,它继续执行下一个命令,而我希望脚本停止,因为第 30 行中的退出命令也应该被执行。
答案 0 :(得分:4)
我怀疑您的主题 Stop a bash script from inside an awk command
是什么让您投票反对,因为试图从 awk 脚本内部控制调用 awk 的外壳程序是您不能也不应该尝试这样做的事情控制反转的坏情况,例如调用 C 中的函数来做某事,并且该函数决定退出整个程序而不是仅仅返回失败状态,因此调用代码可以决定在该失败时做什么(例如执行恢复操作和然后再次调用该函数)。
您似乎将退出 awk 脚本与退出 shell 脚本混淆了。如果你想在 awk 脚本以失败状态退出时退出你的 shell 脚本,那么你需要编写 shell 代码来告诉 shell 这样做,例如:
whatever | awk 'script' || exit 1
或者喜欢它:
whatever | awk 'script' || { ret="$?"; printf 'awk exited with status %d\n' "$ret" >&2; exit "$ret"; }
例如:
$ cat tst.sh
#!/usr/bin/env bash
date | awk '{exit 1}' || { ret="$?"; printf 'awk exited with status %d\n' "$ret" >&2; exit 1; }
echo "we should not get here"
$ ./tst.sh
awk exited with status 1