PDA

View Full Version : [ubuntu] bash script; what is the opposite of this



ant2ne
April 19th, 2009, 03:21 AM
I'd like to add an entry for if the ping failed. What would that look like?



PingSERVERNAME()
{
echo Worked!!!
}
ping -c 1 SERVERNAME && PingSERVERNAME

click4851
April 19th, 2009, 04:10 AM
not a programmer yet, but I've written a few scripts. Could you use a timer statement. If no response within say 30 sec, pipes to a message indicating so.

click4851
April 19th, 2009, 04:16 AM
found this .....the -c count option could be used as well, talks about a unaswered ping toward the bottom, and how ping exits. Found it here...

http://linux.die.net/man/8/ping

sarang
April 19th, 2009, 04:21 AM
You want something like this:



ping_server ()
{
ping -c 1 "$1"
return_value="$?"
if [ "$return_value" != "0" ]

then
echo "Ping failed."
else
echo "Ping succeeded."
fi
}

ant2ne
April 19th, 2009, 10:27 PM
thanks sarang. That looks more complicated than my current scripting level. I like it though!!

hovzio
April 19th, 2009, 11:12 PM
hi, this will work,


ping -c 1 server 2> /dev/null >&2 && echo "worked" || echo "it didnt work"

You can replace the echo commands with funktions if you like. The command following the || is only executed if the last exit status does not equal 0. You can also alias this command to a single server, or put it in a function :)


pingserver ()
{
ping -c 1 "$1" 2> /dev/null >&2 && echo "worked" || echo "it didnt work"
}

sarang
April 24th, 2009, 07:45 AM
thanks sarang. That looks more complicated than my current scripting level. I like it though!!

By convention, programs return an exit status of zero upon successful planned exit and other non-zero exit status (often used as an errorcode to find problems) if an error is encountered. The last exit status is automatically assigned to the special variable $? by the shell. The bash function that I posted earlier just copies $? into $return_value for easy readability and then compares it to zero to determine success. (The != operator is 'not equal'). The use of quotes is analogous to casting everything as strings in real programming languages.