- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: How test an exit code from command?
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2003 03:13 AM
11-07-2003 03:13 AM
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....
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2003 03:21 AM
11-07-2003 03:21 AM
Re: How test an exit code from command?
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 as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2003 03:23 AM
11-07-2003 03:23 AM
Solutione.g
ping server1 -n 1 > /dev/null
if [ $? -eq 0 ]
then
echo "it worked"
else
echo "Oh dear, it didn't work"
fi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2003 03:24 AM
11-07-2003 03:24 AM
Re: How test an exit code from command?
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2003 03:33 AM
11-07-2003 03:33 AM
Re: How test an exit code from command?
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...!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2003 05:02 AM
11-07-2003 05:02 AM
Re: How test an exit code from command?
JP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2003 06:16 AM
11-07-2003 06:16 AM
Re: How test an exit code from command?
$ 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..?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2003 06:47 AM
11-07-2003 06:47 AM
Re: How test an exit code from command?
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-07-2003 07:47 AM
11-07-2003 07:47 AM
Re: How test an exit code from command?
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