LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 07-07-2015, 06:06 AM   #1
robcar
LQ Newbie
 
Registered: May 2015
Posts: 6

Rep: Reputation: Disabled
Bash: handling an exit code


Hello,
I'm writing a bash script to keep ethernet switch counters under control via SNMP queries.
At some point this script connects via snmpwalk:

Code:
# Get array port index
arrIndex=(`snmpwalk -v 2c -c $community $switchIpAddr $etherStatsEntry.1 | awk '{print $4;}'`)

# Is it possibile to get answers from switch?
# The exit state of the last command is evaluated '0' if True or '1' if False and is intercepted by '$?'
if [ $? -ne 0 ]; then
   # Something's wrong
   echo "Wrong IP address or SNMP not configured for switch."
   exit $EXIT_UNKNOWN
fi
If the switch IP is wrong or if SNMP on the switch is not configured I got:

Code:
Timeout: No Response from 10.68.254.245
How can I handle this exception in the script?
The code above doesn't work, because I always get '0' exit code.
I noticed that:

Quote:
arrIndex=(`snmpwalk -v 2c -c $community $switchIpAddr $etherStatsEntry.1 | awk '{print $4;}'`)
and
Quote:
snmpwalk -v 2c -c $community $switchIpAddr $etherStatsEntry.1 | awk '{print $4;}'
exit with '0'.

Only
Code:
snmpwalk -v 2c -c $community $switchIpAddr $etherStatsEntry.1
exits with '1' in case the IP is wrong.

I would prefer not to do a pre-flight check by repeating the command snmpwalk, because that would take extra time.

So the question is: how can I handle the exit code generated by the command snmpwalk above, which is then piped and then assigned to an array?

Thanks.
--
rob
 
Old 07-07-2015, 06:30 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 7.7 (?), Centos 8.1
Posts: 18,115

Rep: Reputation: 2667Reputation: 2667Reputation: 2667Reputation: 2667Reputation: 2667Reputation: 2667Reputation: 2667Reputation: 2667Reputation: 2667Reputation: 2667Reputation: 2667
1. instead of
Code:
(` .. `)
use
Code:
$( ..)
More readable.

2. As you've noticed, if you chain cmds together you get the exit code of the last cmd run (makes sense when you think about it).
Save the result of the first cmd and test the exit code and/or check the o/p text.
 
Old 07-07-2015, 06:58 AM   #3
robcar
LQ Newbie
 
Registered: May 2015
Posts: 6

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by chrism01 View Post
2. As you've noticed, if you chain cmds together you get the exit code of the last cmd run (makes sense when you think about it).
Save the result of the first cmd and test the exit code and/or check the o/p text.
Thanks,
actually I solved by prepending

Code:
set -o pipefail
at the beginning of the script and it seems to be working fine

Quote:
"If pipefail is enabled, the pipeline’s return status is the value of the last (rightmost) command to exit with a non-zero status, or zero if all commands exit successfully."
 
  


Reply

Tags
bash, exit status


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
exit(communication error), terminated by calling exit(), return code: 255 miliboy Linux - Newbie 11 02-27-2013 02:51 PM
Catch exit code before pipe in bash script? prl77 Linux - General 4 05-24-2012 01:09 PM
Log what exits in bash script. What causes exit code thats not 0? Trailsmoke Programming 2 09-25-2008 04:07 AM
Exception Handling (difference between strderr, exit, abort, return) Darkhack Programming 1 02-27-2007 04:36 PM
Bash: Print usage statement & exit; otherwise continue using Bash shorthand operators stefanlasiewski Programming 9 02-07-2006 06:20 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 09:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration