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 11-19-2008, 01: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, 01:29 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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, 01:30 PM   #3
jailbait
LQ Guru
 
Registered: Feb 2003
Location: Blue Ridge Mountain
Distribution: Debian Jessie, Linux Mint 17
Posts: 7,793

Rep: Reputation: 292Reputation: 292Reputation: 292
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, 01:33 PM   #4
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,038

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:

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


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


All times are GMT -5. The time now is 07:55 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration