LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-06-2010, 02:25 PM   #1
gstt
LQ Newbie
 
Registered: Oct 2010
Posts: 2

Rep: Reputation: 0
how to use pipe (|) in a one-line bash command


I am writing a script to put a bunch of files in each subdirectory in a .tar.gz. Here is what I have right now:

for a in `ls -d */`;
do
echo $a;
f1=`find $a -regex '.*ABC' | xargs echo`;
f2=`find $a -regex '.*DEF' | xargs echo`;
f="$f1 $f2";
echo $f;
tar czvf $a.tar.gz $f
done;

But I don't know how to put this script into a one-line bash script. I think my major question is about "|" in the one-line script. Could anyone help me with this?
 
Old 10-06-2010, 02:34 PM   #2
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Your question is a little bit confusing in that the pipe doesn't have much to do with whether or not this script is a one-liner or a 10-liner. You can put all that code on a single line, provided that individual commands are separated appropriately with semicolons like they are. But the two pipes you are currently using, will still be doing the same thing they do now; they pipe the output from `find` into `xargs`.

I figure that your whole command could be shortened a little bit somehow, perhaps by using only `find` rather than using the loop with `ls`, and maybe other change(s), but please explain a little more what exactly you wish to do? Note that jamming some code down into one line doesn't necessarily make it better but can make it harder to figure out what the code does!
 
1 members found this post helpful.
Old 10-06-2010, 02:50 PM   #3
gstt
LQ Newbie
 
Registered: Oct 2010
Posts: 2

Original Poster
Rep: Reputation: 0
Yes. You are absolutely right. On a second try, I found there is nothing wrong to put | in a line, as in my previous script.

At first, I am worried about what is the scope of command as the input to the pipe. For example

ls d1; ls d2 | xargs echo;

From what I tested, only ls d2 is input to xargs echo.

So I think my following question now is if there is syntax to set several commands as input to the pipe. For example, like here, I want to put the output of ls d1 and ls d2 to the pipe of xargs echo.

Thanks!
 
Old 10-06-2010, 03:05 PM   #4
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
To my understanding/knowledge, you cannot pipe two individual processes into one pipe. You could make a FIFO or something of this nature, and send data into from several sources, and retrieve the data using a single process. The data would come out of the FIFO (of course) in the same way it went in; i.e. if it went in scrambled, it would come out scrambled.

Perhaps this might interest you:
Code:
for a in `ls -d */`; do
  f1=`find $a -regex '.*ABC'
  f2=`find $a -regex '.*DEF'
  f="$f1 $f2";
  echo $f;
done; | xargs -I {} tar czvf $a.tar.gz {}
Note that this code is still a little convolved, and I don't totally get what exactly you're trying to do. And, this code as it's written, may not even work - I just stuck it together to show that you can output the results of the entire contents of the code inside the loop, through a pipe into a different command.

A simpler demo:
Code:
for x in 2 3 4; do
    echo $x
    echo $((x+3))
done | xargs -I {} echo "hello, the number is {}"
Again, just a demonstration concept - it doesn't do anything useful.
 
Old 10-06-2010, 07:54 PM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Code:
tar zcvf filename.tgz --recursion *ABC *DEF
 
  


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
Bash Command Line Editor, while typing run another command before executing current? gumaheru Linux - General 5 04-13-2010 11:21 AM
slackware command line pipe-ing issue dv105v Linux - Newbie 2 11-19-2008 09:24 AM
BASH command substitution that starts with a pipe | Kristofer Programming 4 02-27-2007 05:52 PM
Command-line paranoia: Converting a pipe to stdin asciimonster Linux - General 3 06-28-2004 04:57 AM


All times are GMT -5. The time now is 07:32 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration