Shell Test Issue --[: too many arguments

Today, I'm going to write small shell program to verify the demo servers if alive.  

This is the script:

 

#!/bin/sh

echo "Check Servers status startup...."

MAIL_TITLE="OSL OO DEMO SERVERS Check Failed!"

VM03="oardc-vm03.cn.oracle.com"

FOUND=`ping $VM03 -c 2 | grep '2 received,'`

if [ -n $FOUND ];
then
	echo $VM03' is OK!' | mailx -s "Test OK" yonghui.feng@oracle.com
else
 echo $VM03' is Failed!' | mailx -s "$MAIL_TITLE" yonghui.feng@oracle.com
fi

echo "Check Servers status done. check your email"

 

 When I run the script in command line, I got the error message:

 

Check Servers status startup....

./test.sh: line 11: [: too many arguments

 Check the line 11:

 

if [ -n $FOUND ];

 

 Why it will cause the problem?

 

 That is the reason:

 when I print the variable:

 

[root@xxxadmin]# echo $FOUND
2 packets transmitted, 2 received, 0% packet loss, time 1000ms

 convert this variable to the expression: if [ -n $FOUND ];

 

 it will be: if [ -n 2 packets transmitted, 2 received, 0% packet loss, time 1000ms

 if you run the command:

 

 

[root@xxxadmin]# [ -n 2 packets transmitted, 2 received, 0% packet loss, time 1000ms ]
-bash: [: too many arguments

 So we should wrap the variable with "

 

 

if [ -n "$FOUND" ];

 Notice the ".

 

That will work well.

 

this is the final version:

 

 

#!/bin/sh

echo "Check Servers status startup...."

MAIL_TITLE="OSL OO DEMO SERVERS Check Failed!"

VM03="oardc-vm03.cn.oracle.com"

FOUND=`ping $VM03 -c 2 | grep '2 received,'`

if [ -n "$FOUND" ];
then
	echo $VM03' is OK!' | mailx -s "Test OK" yonghui.feng@oracle.com
else
 echo $VM03' is Failed!' | mailx -s "$MAIL_TITLE" yonghui.feng@oracle.com
fi

echo "Check Servers status done. check your email"
 

 

 


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值