Breaking out of a pipe


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Breaking out of a pipe
# 1  
Old 02-12-2009
Breaking out of a pipe

I have the following command in a Bash shell script:

who | grep -w $1 | some other commands

If grep fails, 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.
# 2  
Old 02-12-2009
what do you mean "fails" ? $1 is blank??
returns nothing???

why use grep if you still want to pass everything else to "some-other-commands" ?

you could just run things twice.

who isn't all that expensive a utility.
# 3  
Old 02-12-2009
Fails as in, grep doesn't find any results.

Then output an error message such as, "User is not logged on"

Quote:
why use grep if you still want to pass everything else to "some-other-commands" ?
Er... why not? Is there another command I can use?

I don't want to run things 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 because user is not logged on, then output an error message. After grep, I retrieve the tty(s) of the user(from the grep'ed results), and if all his ttys are non-writeable, 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.
# 4  
Old 02-13-2009
ok. makes more sense.
can't be done.

you must use temp files.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to ignore Pipe in Pipe delimited file?

Hi guys, I need to know how i can ignore Pipe '|' if Pipe is coming as a column in Pipe delimited file for eg: file 1: xx|yy|"xyz|zzz"|zzz|12... using below awk command awk 'BEGIN {FS=OFS="|" } print $3 i would get xyz But i want as : xyz|zzz to consider as whole column... (13 Replies)
Discussion started by: rohit_shinez
13 Replies

2. Shell Programming and Scripting

Breaking a pipe

Here's my code - for File in "${Files}"; do uuencode "${File}" "$(basename ${File} 2>&-)" 2>&-; done | mailx -s "subject" "a@b.c" Now I want to know if there a way to *not* send an email if the "Files" variables turns out to be blank. Any suggestions? Also, just to clarify, I... (4 Replies)
Discussion started by: nexional
4 Replies

3. UNIX for Dummies Questions & Answers

Breaking up at the second occurrence

hi, My input is: 123 1234|123|123|123 123|123|456 123|123|12 12 Expected output is: 123 1234|123 123|123 123|123 456 123|123 12 (1 Reply)
Discussion started by: pandeesh
1 Replies

4. Shell Programming and Scripting

Replace pipe with Broken Pipe

Hi All , Is there any way to replace the pipe ( | ) with the broken pipe (0xA6) in unix (1 Reply)
Discussion started by: saj
1 Replies

5. Shell Programming and Scripting

breaking for loop

Dear Friends, Here I need your guidance once again. I have for loop which check all files in a folder for a particular string. If the string is found in a file it returns value other than 0 else returns 0 value in variable t2. At times the string which we are looking for is in first file... (1 Reply)
Discussion started by: anushree.a
1 Replies

6. Shell Programming and Scripting

Breaking out of loop

I have a main script with while loop having for loop inside. Again in for loop based on if condition few functions will be called. So when a function is called for certain condition it should come out from the main for loop and should continue with while loop. Let me explain with example here: I... (6 Replies)
Discussion started by: vpv0002
6 Replies

7. Shell Programming and Scripting

File breaking

Hey, I have to take one CSV file and break into more files. Let's I have a file prices.csv and the data in the file like 1,12345 1,34567 1,23456 2,67890 2,77720 2,44556 2,55668 10,44996 based on the first column, I want to create files. in this example 1 is repeated three times... (12 Replies)
Discussion started by: bond2222
12 Replies

8. Shell Programming and Scripting

Breaking line

My input file is like USER_WORK.ABC USER_WORK.DEF I want output file like ABC DEF (4 Replies)
Discussion started by: scorp_rahul23
4 Replies

9. Linux

breaking out of while loop

Hi , I am running this script ( pasting only error code ) to generate some ddl definition for tables . But what I want is to break out of the db2look part when the base_table is not like DIM_$TN or FACT_$TN . After this it should come back to while loop to read the next TN . I read the other... (3 Replies)
Discussion started by: capri_drm
3 Replies

10. HP-UX

Breaking Mirror

Can some one point this UNIX newbie to a web site or directions on the steps needed to break a mirror in HP-UNIX to change a bad hard drive. (4 Replies)
Discussion started by: egress1
4 Replies
Login or Register to Ask a Question