检查从命令的输出零线通过SSH [英] Check for zero lines output from command over SSH
问题描述
如果我这样做,而网络出现故障,那么零的情况下将被执行,它不应该的。
If I do the following, and the network is down, then the zero case will be executed, which it shouldn't.
case "$(ssh -n $host zfs list -t snapshot -o name -H | grep "tank/fs" | wc -l | awk '{print $1}')" in
0) # do something
;;
1) # do something else
;;
*) # fail
esac
在我检查,我可以SSH到 $主机
脚本较早,但今天我发现了这个问题,去哪儿网我的支票后立即失败。
Earlier in the script I check that I can SSH to $host
, but today I found this problem, where the network failed right after my check.
如果我检查从SSH命令的返回值,那么我将永远,因为它是最后执行获得 AWK
的返回值。
If I check the return value from the SSH command, then I will always get the return value from awk
as it is executed last.
问
我如何确保我其实算零线的 ZFS
输出,并从失败的SSH连接没有零线?
How do I insure that I actually count zero lines that zfs
outputted, and not zero lines from a failed SSH connection?
推荐答案
说:
set -o pipefail
在你的脚本的开头(或案前
语句)。
此外,执行案前检查命令的返回code
语句:
set -o pipefail
$value=$(ssh -n $host zfs list -t snapshot -o name -H | grep "tank/fs" | wc -l | awk '{print $1}')
if [ $? == 0 ]; then
case $value in
...
esac
fi
从手动:
pipefail
如果设置,管道的返回值是最后的值
(最右边)的命令与非零状态退出,或零如果所有
在成功地管道出口命令。此选项被禁用
默认值。
If set, the return value of a pipeline is the value of the last (rightmost) command to exit with a non-zero status, or zero if all commands in the pipeline exit successfully. This option is disabled by default.
这篇关于检查从命令的输出零线通过SSH的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!