LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 11-19-2008, 12:15 PM   #1
vickylife
LQ Newbie
 
Registered: Nov 2008
Posts: 6

Rep: Reputation: 0
checking for return status between multiple commands


i have to run set of commands

command1
command2
command3
command4

Now Whenever any of these command fails i should quit while capturing error message.
Is there a better way then checking for $? after each command.
 
Old 11-19-2008, 12:29 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Maybe something like this?
Code:
command1 2> error.log || exit
command2 2> error.log || exit
command3 2> error.log || exit
command4 2> error.log || exit
 
Old 11-19-2008, 12:30 PM   #3
jailbait
LQ Guru
 
Registered: Feb 2003
Location: Virginia, USA
Distribution: Debian 12
Posts: 8,337

Rep: Reputation: 548Reputation: 548Reputation: 548Reputation: 548Reputation: 548Reputation: 548
If you want to delay your error checking until after all of the commands have been executed then you will have to save $? in separate variables after each command.

---------------------
Steve Stites
 
Old 11-19-2008, 12:33 PM   #4
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
If you want to check each command for error codes, then no I would think you would need to check the value of $? for each command.

If you want the fourth command to run only if the previous three ran succesfully you could try:

command1&&command2&&command3&&command4

command2 would only run if command1 was successful.
command3 would only run if command2 was successful.
command4 would only run if command3 was successful.

if $? is 0 then all commands were successful, otherwise one failed but you have no way (that I know of) to determine which command failed.
 
  


Reply



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
Problem in checking function return value viveksnv Programming 3 10-02-2008 01:17 AM
Official command return status specification?? kinzlaw Linux - General 1 03-11-2006 02:38 AM
Checking device status neerukamra Linux - General 1 09-06-2004 01:12 AM
command(s) for checking ip status stupidloser Linux - Wireless Networking 5 08-10-2004 10:21 AM
checking the status of PPP0 pudhiyavan Linux - Networking 6 01-14-2004 02:47 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:36 PM.

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