Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 11-19-2008, 12:15 PM   #1
LQ Newbie
Registered: Nov 2008
Posts: 6

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

i have to run set of commands


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
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1980Reputation: 1980Reputation: 1980Reputation: 1980Reputation: 1980Reputation: 1980Reputation: 1980Reputation: 1980Reputation: 1980Reputation: 1980Reputation: 1980
Maybe something like this?
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
LQ Guru
Registered: Feb 2003
Location: Blue Ridge Mountain
Distribution: Linux Mint 17, Debian 8
Posts: 7,865

Rep: Reputation: 311Reputation: 311Reputation: 311Reputation: 311
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
Senior Member
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 97
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:


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.


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
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 > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:43 AM.

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