Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PING !! 1

Status
Not open for further replies.

071

MIS
Aug 9, 2000
153
The following script works but unfortunatly overwrites itself in the 'down' file.

ping 'xxx.xx.x.xx' 2
[ $? != 0 ] && echo "No reply from server" > down
ping 'xxx.xx.x.xx' 2
[ $? != 0 ] && echo "No reply from server" > down

etc....

Any suggestions ???




 
Code:
cat /dev/null > down
ping 'xxx.xx.x.xx' 2
[ $? != 0 ] && echo "No reply from server" >> down
ping 'xxx.xx.x.xx' 2
[ $? != 0 ] && echo "No reply from server" >> down

Lots of other ways to achieve this also!

Greg.
 
Just tried this but to no avail....must be doing something wrong ??

#!/bin/ksh
echo The following is a list of active/inactive servers

cat /dev/null > down
ping server1 2
[ $? != 0 ] && echo "No reply from server1" >> down
ping server2 2
[ $? != 0 ] && echo "No reply from server2" >> down
ping server3 2
[ $? != 0 ] && echo "No reply from server3" >> down
ping server4 2
[ $? != 0 ] && echo "No reply from server4" >> down
ping server5 2
[ $? != 0 ] && echo "No reply from server5" >> down


 
Not sure as I'm at home and can't test, but you might need braces round the expression (the [ ... ] && .... is just a shortcut if .. then .. fi statement)
Code:
#!/bin/ksh
echo The following is a list of active/inactive servers

cat /dev/null > down                                   
ping server1 2                                           
[ $? != 0 ] && {echo "No reply from server1" >> down}
ping server2 2                                            
[ $? != 0 ] && {echo "No reply from server2" >> down}
Failing that, you could try
Code:
#!/bin/ksh
echo The following is a list of active/inactive servers

cat /dev/null > down                                   
ping server1 2                                           
if [ $? != 0 ]
then
  echo "No reply from server1" >> down
fi

Greg.

 
how about

ping server1 2 || echo "server1 is down" >> down
ping server2 2 || echo "server2 is down" >> down
ping server3 2 || echo "server3 is down" >> down

Mike
michael.j.lacey@ntlworld.com
 
Mike ... curious ... what does the || signify?

Greg.
 
Thanks guys, that seems to have done the job nicely.
 
Greg,

It's just a shorthand way of saying exactly what you said.

[tt]command1 || command2[/tt]

is equivalent to...

if command1 does not succeed run command2

There is also && as well

[tt]command1 && command2[/tt]

is equivalent to...

if command1 does succeed run command2

Mike
michael.j.lacey@ntlworld.com
 
I don't know who gave me the vote but thanks -- and it's one of my favorite tricks as well ;-)
Mike
michael.j.lacey@ntlworld.com
 
Good explanation of the && and ||, but I'm surprised that nobody mentioned it basically means AND and OR.
command1 || command2 is basicaly a logical OR test.
A command returns a result of either it worked or didn't.
Most scripting systems only evaluate a line to the point where the "outcome" can be identified. In the case of an "OR" statement, the result is TRUE if any of the statements are TRUE, and FALSE only if all statements are FALSE. Thus Command1 || Command2 will execute the first command and only continue to the next command if the result is FALSE (first command failed). If the first command suceeds...it is not necessary to evaluate the additional commands since the OR clause only requires one positive result.
The AND conjunction (&&) requires ALL commands return true and thus will evaluate every Command until one does not succeed or statements are exhausted. Command2 will only be executed if Command1 succeeds.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top