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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get Return Value 1

Status
Not open for further replies.

Michael42

Programmer
Oct 8, 2001
1,454
US
I obviously have a big hole in my UNIX knowledge on this one.

In a Bourne shell script I want to ping a hostname to see if it is there. I know I can do it this way:
Code:
STATUS=`ping $HOSTNAME`
echo $STATUS

...and then grep for "alive" etc.

I cannot find\remmeber how to get the return value. I want to use it like this:
Code:
ping $HOSTNAME
if [ <what goes here> -ne 0 ];
   echo "Host not available."
fi

What is the character that I need that will have the return value of ping?

Thanks,

Michael42
 
Salem,

Perfect - thanks very much for posting. :)

-Michael42
 
If you just need the exit status, and code your if/then/else/fi construct around that status, usually you don't want the output of the "test" program:

ping $HOSTNAME >/dev/null 2>&1
if [ $? -eq 0 ]
then
echo host $HOSTNAME is reachable
else
echo host $HOSTNAME is not reachable
fi


HTH,

p5wizard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top