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"