繁体   English   中英

存储无法执行的命令的输出

[英]storing output of a command that fails to execute

我正在尝试将命令的输出存储到脚本中的变量,然后根据存储在变量中的输出来打印结果。

例如,在此脚本中,我尝试打印从lab.txt文件读取的少数系统上安装的代理的状态。 如果状态与“正在运行”匹配,则打印“已安装代理”。

~]#  cat lab.txt
192.168.1.1
192.168.1.2

这是脚本-

#!/bin/bash
while read host; do
status=$(ssh -n root@$host /opt/agent/bin/agent.sh status | awk 'NR==1{print $3 $4}')

if [ $status != isrunning ]
then
echo "agent is not installed"
else
echo "agent is installed"
fi
done < lab.txt

这里的问题是,如果命令返回错误,因为/ opt / agent目录在系统192.168.1.2上不存在,则不会打印“未安装代理”消息。 这有什么问题吗?

~]# ./script.sh

   root@192.168.1.1:

   agent is installed

   root@192.168.1.2:

   bash: /opt/agent/bin/agent.sh: No such file or directory
   ./script.sh: line 5: [: !=: unary operator expected
   agent is installed

$status未初始化,因此不满足条件[ status != isrunning ] 就像是:

if [ "$status" != isrunning ]

应该解决这个问题,也摆脱您unary operator expected的错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM