我有以下工作脚本:
for i in `cat list` ; do
ssh root@$i "uname -n ; cat /opt/Symantec/virusdefs/definfo.dat ; service rtvscand status ; echo ....................................................................." ;
done | tee /tmp/symantec_info.`date +"%m%d%y"`
它产生如下所示的输出:
server1.local
[DefDates]
LastDefs=20150824.003
CurDefs=20150826.002
rtvscand is running
.....................................................................
server2.local
[DefDates]
LastDefs=20150609.001
CurDefs=20150610.004
rtvscand is not running
.....................................................................
server3.local
[DefDates]
LastDefs=20150607.001
CurDefs=20150608.004
rtvscand is running
.....................................................................
但是,我希望输出仅显示正在运行 rtvscand 的服务器。换句话说,我希望输出看起来完全像:
server1.local
[DefDates]
LastDefs=20150824.003
CurDefs=20150826.002
rtvscand is running
.....................................................................
server3.local
[DefDates]
LastDefs=20150607.001
CurDefs=20150608.004
rtvscand is running
.....................................................................
答案1
您可以根据以下输出做出决定service rtvscand status
:
[ "$(service rtvscand status)" = 'rtvscand is running' ] && do_what_you_want
这意味着如果 的输出rtvscand is running
是rtvscand is running
那么我们将做一些事情,否则不会。
所以你的脚本可以采用以下形式:
while IFS= read -r name; do
ssh root@"$name" '[ "$(service rtvscand status)" = "rtvscand is running" ] && \
{ uname -n; cat /opt/Symantec/virusdefs/definfo.dat ;}'
done <list.txt