Operating System - HP-UX
1833875 Members
3115 Online
110063 Solutions
New Discussion

Re: How test an exit code from command?

 
SOLVED
Go to solution
GerGon
Regular Advisor

How test an exit code from command?

Hi everybody,

Is there some way to test an output exit code from one command?
I'm trying to test a ping command, I don't need any output command properly, ONLY I need test if the command was successful or not?
i.e:
1.) ping server1 -n 1
(test it with some like follow)
ping server1 -n 1
if [ $? ] then echo "successfull"
else echo " The is offline.. Pl, check it out"
fi

2.)
mt rew
if ! test $? then "Device error!!"
----------------------------------------
Ok, I know abou and always used:
f1=`ls -l | wc -l`
if [ $f1 -gt 1 ] then echo "files yet"
----------------------------------------
But I don't want use more that way.. I want to test the command directly ??????

I need to know the i.e. number 1 and 2 only..

Thanks....
8 REPLIES 8
John Poff
Honored Contributor

Re: How test an exit code from command?

Hi,

You could try this way:

ping server1 -n 1 >>/dev/null || echo "server1 is offline, check it out"

The '||' is a logical 'or' and the echo will only be executed if the ping command fails.

JP

Mark Grant
Honored Contributor
Solution

Re: How test an exit code from command?

Most unix commands will finish with an exit status but if you don't want any output, just redirect it somewhere else

e.g

ping server1 -n 1 > /dev/null
if [ $? -eq 0 ]
then
echo "it worked"
else
echo "Oh dear, it didn't work"
fi
Never preceed any demonstration with anything more predictive than "watch this"
A. Clay Stephenson
Acclaimed Contributor

Re: How test an exit code from command?

First of all, you should get into the habit of immediately capturing the value of ${?}.
ping server1 -n 1
STAT=${?}

This lets you check ${STAT} possibly more convenient. You are doing things correctly but you have chosen commands that don't always return a useful exit status. For example, ping badhostname -n 1 will return a non-zero exit status but ping goodhostname -n 1 will return a zero exit status but still might not be able to ping the host. You actually have to look at stdout/stderr of some commands for useful ststus messages.

THe convention is that MOST commands return 0 for succcess and that is about as goods as it gets.
If it ain't broke, I can fix that.
GerGon
Regular Advisor

Re: How test an exit code from command?

John,

There is something wrong in your command, it is very interesting to me but doesn't work...

How it is?

*** Thanks to all four answers...!!

John Poff
Honored Contributor

Re: How test an exit code from command?

What error are you getting when you try the command? The '||' is two vertical bars or pipe symbols[shift backslash on most keyboards].

JP
GerGon
Regular Advisor

Re: How test an exit code from command?

John I get :
$ echo "$?ng limon -n 1 >>/dev/null || echo "server1 is offline, check it out""
0ng limon -n 1 >>/dev/null || echo server1 is offline, check it out
$

I think it echoing not right..?
John Poff
Honored Contributor

Re: How test an exit code from command?

Hi,

The start of your command line looks odd. You should be starting with:

ping server1 -n 1 >>/dev/null ...

It looks like you have the echo command at the start of the line?

JP
Bill Hassell
Honored Contributor

Re: How test an exit code from command?

The problem with this test is with ping, not with capturing the exit code. ping has only 3 exit codes and NONE of them cover the success of a pingable host!!! Here are the codes (from the man page):


0 On success.
1 On failure such as unknown host, illegal packet size, etc.
2 On a unreachable host or network.

You see, ping by default will send multiple packets so if you ping 3 times and you get two responses within the ping ICMP test window, then the exit code should be 0.6666 or something similar. If there are duplicate IP's on the same subnet, you might get 200% returns.

But what you want is to know that a single ping did return. That is NOT exit 0 which means that a ping was sent out after validating that basic network name resolution and packet size is correct. To determine that a single ping returned one and only one packet, you'll have to capture the 2nd line from the end and see if 0% exists:

PERCENT=$(ping 1.2.3.4 -n 1 | awk '/transmitted/ {print $7}')

Now this line can't handle a failure from ping since the return code is from awk, not ping. So for scripting, you can check if $PERCENT is null...if so, then ping did not return a message with "transmitted" in it, thus a basic networking error occurred (ping errors 1 or 2). If $PERCENT="0%" then all is well, all else is a problem.


Bill Hassell, sysadmin