Discussion:
pipe as argument?
(too old to reply)
Berk Birand
2004-04-04 11:02:04 UTC
Permalink
Hi,

AS I was reading a book on shell, I came up with a lot of redirection
operators that I did not know. After seeing all these, it occurred to me
that perhaps a feature that I was looking for might have its own operator.

Is it possible to pipe a commands output to another commands command-line
arguments instead of its standard input?

For instance when one does a listing with a simple ls, one gets a bunch of
filenames, such as:
$ ls
COPYING.gz ChangeLog.gz Copyright.gz Problems.gz README.gz timespec.gz

I would like to send these files as arguments to cat in order to print
them all. Now I know that for this particular case, this may be
accomplished by 'cat ./*' , but there are other circumstances in which
such an alternative is unavailable.

Any Ideas?
Thanks
BB
Urs Thuermann
2004-04-04 11:26:46 UTC
Permalink
Post by Berk Birand
For instance when one does a listing with a simple ls, one gets a bunch of
$ ls
COPYING.gz ChangeLog.gz Copyright.gz Problems.gz README.gz timespec.gz
I would like to send these files as arguments to cat in order to print
them all.
cat `ls`


urs
Johannes Kloos
2004-04-04 11:25:36 UTC
Permalink
Berk Birand <***@NOSPAMyahoo.com> wrote:
[...]
Post by Berk Birand
Is it possible to pipe a commands output to another commands command-line
arguments instead of its standard input?
You may want to look at the backtick operator:
foo `bar`
calls foo with bar's output as arguments.
Web Surfer
2004-04-04 14:51:20 UTC
Permalink
[This followup was posted to alt.unix.wizards]
Post by Berk Birand
Hi,
AS I was reading a book on shell, I came up with a lot of redirection
operators that I did not know. After seeing all these, it occurred to me
that perhaps a feature that I was looking for might have its own operator.
Is it possible to pipe a commands output to another commands command-line
arguments instead of its standard input?
For instance when one does a listing with a simple ls, one gets a bunch of
$ ls
COPYING.gz ChangeLog.gz Copyright.gz Problems.gz README.gz timespec.gz
I would like to send these files as arguments to cat in order to print
them all. Now I know that for this particular case, this may be
accomplished by 'cat ./*' , but there are other circumstances in which
such an alternative is unavailable.
Any Ideas?
Thanks
BB
use the backtick operator as follows :

cat `ls *.h`
Nick Upson
2004-04-04 15:53:40 UTC
Permalink
Post by Berk Birand
I would like to send these files as arguments to cat in order to print
for file in `ls`
do
lp $file
done

# Note the quotes around ls, it uses the ` NOT the ' character
--
Nick
-----We Solve your Computer Problems---
Founder & Listowner of the Prolifics User Group
Panther, Ingres, UNIX, Interbase, Firebird - Available Shortly
Bill Marcum
2004-04-05 06:47:41 UTC
Permalink
On Sun, 04 Apr 2004 16:53:40 +0100, Nick Upson
Post by Nick Upson
Post by Berk Birand
I would like to send these files as arguments to cat in order to print
for file in `ls`
do
lp $file
done
# Note the quotes around ls, it uses the ` NOT the ' character
In bash, ksh and some other shells you can also write
for file in $(ls)
--
Nothing is illegal if one hundred businessmen decide to do it.
-- Andrew Young
Berk Birand
2004-04-07 12:00:50 UTC
Permalink
Thanks for the replies!

I have also found a little tool that precisely does that, called xargs.
You simply pipe the standard output of a command to "xargs command", and
it runs "command" with those arguments.

I also have found that the -exec feature of find can be extremely helpful
if one knows how to use it. I think I will spend some time mastering that
tool...

Thanks again,
BB

Loading...