can someone please help me fix the below one liner?
what i want to do is run a command, and if that command is successful, pipe the output of the command to another command. i'd prefer not to use any temp files for this.
who -blahblah ; if [ $? -ne 0 ] ; then echo exit; fi | egrep username
I do not know if what you just posted is a "loose" interpretation of what you want but not real or you have actually tried to run that command.
You may do it as:
who | grep username
You can not check if the command is successful and then try to pipe it to another program. That's not how it works.
When you use a PIPE both programs run in parallel and the output of the first becomes the input of the second.
You haven't said what operating system or shell you're using.
If you're on a system using a recent version of bash , you might want to look at the bash manage for the definition of the PIPESTATUS array. With the commands:
who | grep name
echo ${PIPESTATUS[@]}
with a recent bash you would get the output from the grep followed by a line showing the exit value of both commands in the pipeline. For example:
bash-3.2$ who | grep dwc
dwc console Dec 8 21:19
dwc ttys000 Dec 8 21:22
dwc ttys001 Dec 8 21:22
dwc ttys002 Dec 8 21:22
dwc ttys003 Dec 8 21:22
dwc ttys004 Dec 8 21:22
dwc ttys005 Dec 8 21:22
dwc ttys006 Dec 8 21:22
dwc ttys007 Dec 8 21:22
bash-3.2$ echo ${PIPESTATUS[@]}
0 0
bash-3.2$ who | grep unknown
bash-3.2$ echo ${PIPESTATUS[@]}
0 1
bash-3.2$ who -x | grep dwc
who: illegal option -- x
Usage: who [-abdHlmpqrsTtu] [file]
who am i
bash-3.2$ echo ${PIPESTATUS[@]}
1 1
bash-3.2$