Proper chaining and piping of commands


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Proper chaining and piping of commands
# 1  
Old 01-07-2016
Proper chaining and piping of commands

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.

Code:
who -blahblah ; if [ $? -ne 0 ] ; then echo exit; fi | egrep username

# 2  
Old 01-07-2016
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:
Code:
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.

Last edited by Aia; 01-08-2016 at 12:41 AM..
# 3  
Old 01-08-2016
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:
Code:
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:
Code:
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$

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Piping commands using xargs

Need help in piping commands using xargs I have several .tar.gz files that I need to list the folder content in a subdirectory. For example, a.tar.gz b.tar.gz c.tar.gz The following command works great for each .tar.gz file but it's a pain to run the tar command for each file. tar -tf... (11 Replies)
Discussion started by: april
11 Replies

2. Shell Programming and Scripting

Chaining together exec within find

I need to do the following with a find command on my AIX box Find all files that are -type f Then do the following steps:- Take a listing of them, and write them to a log in /directory/backup/log Tar them up in /directory/backup/tar and remove the files. Here is what I have... (22 Replies)
Discussion started by: jeffs42885
22 Replies

3. UNIX for Dummies Questions & Answers

Piping commands

Hi I am tryin to undertand piping command1|command2 from what i learn output of cammand 2 is an intput for command 1 right? If so . What dose next sequence do cat f1 >> f2 | grep '^' I think it takes context of f1 and Concatenate's it to f2 and then looks for ....i don't know..... (7 Replies)
Discussion started by: iliya24
7 Replies

4. Shell Programming and Scripting

[Solved] BASH - chaining TEST and COMMAND with && and II

Can you explain what this line of script is doing. What I have understood is : -- variable C is the name of a software which is either not installed, so it must be installed or allready installed and then should be update if newer version found -- branch B="$B $C" is to install the software --... (4 Replies)
Discussion started by: jcdole
4 Replies

5. UNIX for Dummies Questions & Answers

Piping multiple commands

Using the below code I want to find all .sff files and extract them. This works but it seems very cheap. Is there a safer more efficient way to go about this? #!/bin/bash G1=(/home/dirone) find ${G1} -type f -name \*.sff | xargs python /usr/local/bin/sff_extract.py (3 Replies)
Discussion started by: jrymer
3 Replies

6. UNIX for Dummies Questions & Answers

Proper syntax

I'm new to Unix, and just had a quick question. I'm writing a bash script, and I was wondering what proper programming etiquette was for piping. How many pipes is too many pipes? OLDEST=$(find . -maxdepth 1 -type d -newermt 2012-07-01 ! -newermt 2012-07-30 | xargs ls -1td | tail -2) echo... (1 Reply)
Discussion started by: jrymer
1 Replies

7. Shell Programming and Scripting

bash aliases and command chaining with ; (semi-colon)

What am I doing wrong here? Or is this not possible? A bug? alias f='find . >found 2>/dev/null &' f ; sleep 20 ; ls -l -bash: syntax error near unexpected token `;' (2 Replies)
Discussion started by: star_man
2 Replies

8. Shell Programming and Scripting

Piping through commands read as variables from file

This is going to be part of a longer script with more features, but I have boiled it down to the one thing that is presently stumping me. The goal is a script which checks for updates to web pages that can be run as a cron job. The script reads (from a tab-delim file) a URL, an MD5 digest, and an... (1 Reply)
Discussion started by: fitzwilliam
1 Replies

9. UNIX for Dummies Questions & Answers

Proper use of prune...

My goal was to find any directories inside of any directory called "09_Client Original" not modified in the last 30 days. $ find /Volumes/Jobs_Volume/ -type d -name "09_Client Original" -exec find {} -mtime +30 -type d -maxdepth 1 \; The results of this find are passed along in a perl script... (1 Reply)
Discussion started by: guriboy
1 Replies

10. IP Networking

Proper routing

I have a series of new machines that are internet facing (have IP's that are accessible via the 'net) and it has internal facing interfaces. I need to be able to communicate back to the internal network to a specific server which processes monitoring and e-mail traffic. I've been told that I should... (3 Replies)
Discussion started by: BOFH
3 Replies
Login or Register to Ask a Question