1748092 Members
5840 Online
108758 Solutions
New Discussion юеВ

Re: pipe or xargs

 
SOLVED
Go to solution
Maaz
Valued Contributor

pipe or xargs

echo -e "/usr\n/opt\n/bin" > file.txt

1, cat file.txt |ls (doesnt work, this 'ls' will list only the contents of pwd, but doesnt list the output from 'file.txt')
2, cat file.txt |xargs ls (this works, i.e ls will list the out of 'file.txt')

"pipe" pick the output of from the command on left, and make that output as an input to the command on right ?
then why "1" doesnt work as I expect(or as "2" works )

e.g
3, echo 123 | mail -s "testing" root@localhost (it works)
in this case(3), pipe is working as the definition, or simply the same as case "2"

please help/explain

Regards
2 REPLIES 2
Dennis Handly
Acclaimed Contributor
Solution

Re: pipe or xargs

>1) cat file.txt | ls

ls(1) doesn't take its input from stdin, only from command line args. With a real shell (posix/ksh) you can do:
ls $(< file.txt )

>2) cat file.txt | xargs ls (this works

Yes, xargs reads stdin and then invokes ls with command line args.

>"pipe" pick the output of from the command on left, and make that output as an input to the command on right?

Yes but there is no need to use evil cat(1) in your simple cases:
xargs ls < file.txt

>then why "1" doesn't work as I expect

Because "ls < file.txt" doesn't read stdin.

>3) echo 123 | mail -s "testing" root@localhost (it works)
>pipe is working as the definition

The echo/pipe works because mail reads from stdin and has command line options.
Srimalik
Valued Contributor

Re: pipe or xargs

>"pipe" pick the output of from the command on left, and make that output as an input to the command on right?

output and input here refer to the stdout and stdin respectively, stdin is not same as command line args.

If a program uses commandline args u have to use xargs.
If your program works on the input suplied on stdin; a | will be sufficient.

-Sri
abandon all hope, ye who enter here..