LinuxQuestions.org
Visit Jeremy's Blog.
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 02-13-2009, 01:15 AM   #1
ahtoot
LQ Newbie
 
Registered: Jun 2005
Posts: 22

Rep: Reputation: 1
Adding error messages in a pipe


I have the following command in a Bash shell script:

cat file | grep -w $1 | some other commands

If grep does not find any result, an error message is displayed. How do I test if grep fails and still be able to pipe it's output to the rest of the commands?


I have the following solution:

a=`who | grep -w $1`
if [ $? -eq 0 ]
then
display error message
fi
printf "$a" | some other commands


Is this the best solution, or is there another method?
Just in case I have some other set of commands, and the variable is assigned some other output, I read somewhere that its not a good idea to assign variables to a large amount of data, or binary data.

I don't want to run the first portion twice, because there are further error conditions in the subsequent set of commands. The idea is, I have a long sequence of commands, connected with pipes. If the execution fails at grep, then output an error message. After grep, I retrieve further information from the text file, and if some other condition is not met, output another error message, and so on and so forth. There are multiple error conditions in the commands, and if it fails at any point, I would like to know where it fails, and output an appropriate error message.
 
Old 02-13-2009, 02:17 AM   #2
kenneho
Member
 
Registered: May 2003
Location: Oslo, Norway
Distribution: Ubuntu, Red Hat Enterprise Linux
Posts: 655

Rep: Reputation: 40
Hi.

Don't know if this is what you're after, but have you considered rediricting std err to std you, like this:

cat file | grep -w $1 2>&1 | some other commands
 
Old 02-22-2009, 11:40 AM   #3
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
Please put code in "Code:" blocks -- it's much easier to read.

What do you want to do if your grep fails?
Do you still want to pipe its output to the rest of the commands, or only if it succeeds?
 
Old 02-23-2009, 12:58 AM   #4
ahtoot
LQ Newbie
 
Registered: Jun 2005
Posts: 22

Original Poster
Rep: Reputation: 1
Nope, piping the output to the rest of the commands is not necessary if grep fails.

Essentially, what I want to do is something like the following:

command1 | command2 | command3 | command4 | .....

If command2 fails for whatever reason, execute some piece of code and exit;
If command4 fails for whatever reason, execute some piece of code and exit, etc etc.


My question is how to place a conditional within the series of pipes to test for the failure of a command, execute some piece of code if it fails, and still be able to pipe the output to the rest of the commands if it succeeds.
 
Old 02-23-2009, 09:07 AM   #5
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
Code:
command1 | (command2 || exit) | command3 | {command4 || exit} |
This is a quick, untested suggestion. I don't know if the '{', '}' form will even work; or, if it will, if it's preferable to '(', ')'. If there is a preference, it is based on spawning/not spawning subshells.
 
Old 03-09-2009, 11:12 AM   #6
kenneho
Member
 
Registered: May 2003
Location: Oslo, Norway
Distribution: Ubuntu, Red Hat Enterprise Linux
Posts: 655

Rep: Reputation: 40
Quote:
Originally Posted by ahtoot View Post
Nope, piping the output to the rest of the commands is not necessary if grep fails.

Essentially, what I want to do is something like the following:

command1 | command2 | command3 | command4 | .....

If command2 fails for whatever reason, execute some piece of code and exit;
If command4 fails for whatever reason, execute some piece of code and exit, etc etc.


My question is how to place a conditional within the series of pipes to test for the failure of a command, execute some piece of code if it fails, and still be able to pipe the output to the rest of the commands if it succeeds.
I must say that I believe that making a shell-script like you suggested in your first post seems like a more neat way of doing this..
 
  


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
Postfix: User messages are okay; but error messages themselves seem to be bouncing jgombos Linux - Server 3 03-03-2007 03:21 PM
pipe for adding UI on console program okeyla Linux - Newbie 1 09-20-2005 09:36 PM
From where am i getting error messages to /var/log/messages? prabhuacsp Programming 3 02-16-2005 09:59 AM
From where am i getting error messages to /var/log/messages? prabhuacsp Linux - Networking 1 02-16-2005 01:34 AM
Broken Pipe Error ken734 Linux - Newbie 6 09-04-2003 10:54 AM


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