LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 06-30-2010, 12:08 PM   #1
EnderX
Member
 
Registered: Nov 2006
Posts: 63

Rep: Reputation: 15
Using shell command output as input in shell script - how to do?


If what I am asking for is something that should have been obvious, I apologize.

I have been requested to build a script for starting and stopping our main (postgresql) database's slony replication system, said script intended to be run at boot. I've got a shell script working in a sort of skeletal fashion (it does what it's supposed to under the assumption that nothing can go wrong), but I'm wondering about how to set it to catch errors.

The code itself calls a set of shell commands I was given to manually start and stop the replication. The catch is, in the past I have sometimes seen a 'failed to start' message in the output from those, even though it otherwise appeared to have successfully run. My habit is, after seeing this message, to immediately kill replication and attempt to restart it again. Given that the script for this is supposed to be a 'run at boot' task, I need some way to programatically do this.

My actual question is this: How do I have my script look over the output a prior command inside it generated? I'm fairly sure I can just run the stop and start procedures as '$0 stop, $0 start' if needed, but how do I isolate that 'failed' message and return it for code use? I'm assuming grep to isolate, but beyond that I'm in the dark. Even if all you care to offer is the name of the command or commands I need, I will be grateful.

Additionally, if there are any other flaws to my logic, I would greatly appreciate having them pointed out to me before I manage to accidentally mangle the system I'm building this for, and request that you please do so.

Thank you for your consideration.
 
Old 06-30-2010, 12:19 PM   #2
Didier Spaier
Senior Member
 
Registered: Nov 2008
Location: Paris, France
Distribution: Slackware{,64}-{14.1,current} on a Lenovo Thinkpad T61 6457-4XG
Posts: 4,159

Rep: Reputation: 1013Reputation: 1013Reputation: 1013Reputation: 1013Reputation: 1013Reputation: 1013Reputation: 1013Reputation: 1013
It's hard to figure out what you could do not seeing your script as it is.

Could you post it, surrounded by CODE tags to make it easier to read ?
 
Old 06-30-2010, 12:46 PM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,533
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
You could start by looking at actual boot scripts. Mostly they do little error trapping, they simply allow output from the commands they run to go to console where they are visible (assuming no boot splash or effective boot logging). It's helpful if they print a (terse) message saying what they intend to do; this allows a possibly mysterious error message to be rapidly traced to a particular script.

Your ideas of the script analysing the output from commands is more appropriate for a script which is not sending output to console. It's great for rapid troubleshooting of scripts that output to log where more-or-less sophisticated functions to handle messaging/logging can be useful, especially in the develop/test/debug cycle when a script may be run from the command prompt and saves having to consult the log if messages are automatically sent to the controlling terminal when there is one or otherwise to log.

Command output can (almost always) be captured by
Code:
cmd_out=$( <some command> 2>&1 )
after which $? is set to <some command>'s return code and can conveniently saved for later reference with rc=$?.

Now you've got the available data from running the command, what you do with it depends on the command and context -- which outputs and return codes do you want to treat as information, warning or error (or whatever other classification you choose)? In simple cases any non-zero return code is a fatal error so you could use something like
Code:
cmd_out=$( <some command> 2>&1 )
rc=$?
if [[ $rc ]]; then
    echo "Error while <doing something>.  Return code $rc.  Command output:"$(\n)"$cmd_out" >&2
    exit 1  
fi
In more complex cases you might need to parse the output
Code:
cmd_out=$( <some command> 2>&1 )
case "$cmd_out" in
    <OK expression 1> | <OK expression 2> | ... | <OK expression n> )
        <do nothing unless you want to log informational message>
        ;;
    <WARNING expression 1> | ... | <WARNING expression n> ) 
        <log warning message>
        ;;
    <ERROR expression 1> | ... | <ERROR expression n> )
        <log error message and call finalise function or exit>
        ;;
    * )
        <log unhandled error message and call finalise function or exit>
        ;;
esac
It may be useful to include the name of the script -- ${0##*/} -- or the line number -- $LINENO -- in messages.
 
  


Reply


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
shell script question,standard input,output,error cola Programming 4 04-15-2010 01:11 AM
for shell script how to store the output of another task other than this command self vvpp Linux - Newbie 11 11-13-2009 12:28 AM
Help on my Linux Homework ! bash shell script / input-output data etc Bebelindo Programming 2 03-03-2009 12:51 PM
[shell script] execute command and parse output stoiss Programming 2 01-26-2009 01:49 AM
Help!! Shell script to get output of ls -l command into an array kasthana Programming 8 06-01-2008 11:37 PM


All times are GMT -5. The time now is 10:54 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration