Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
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..
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.