Shell tips and tricks (was: cat pipe)
Steven W. Orr
steveo at syslang.net
Fri Oct 5 13:26:38 EDT 2007
On Friday, Oct 5th 2007 at 10:04 -0000, quoth Ben Scott:
=>On 10/4/07, Michael ODonnell <michael.odonnell at comcast.net> wrote:
=>> Bash users can do this:
=>>
=>> cp <( date ) /dev/tty
=>
=> I had to look that up. "Process Substitution". Assuming I
=>understand correctly, that connects the output of the "date" command
=>to a named pipe (say, /tmp/foo), and then substitutes the name of said
=>pipe for the actual argument. So after substitution, the command
=>would look like:
=>
=> cp /tmp/foo /dev/tty
=>
=> Obviously, your example is a pointless example, but even so... I'm
=>trying to think what possible use this would be. Why would you want
=>to use Process Substitution for real?
=>
=> The only thing I could think of would be for lame programs which
=>insist on read/writing to a named file, and will not use stdin/stdout
=>in the manner of a Unix filter. But I've always just done:
=>
=> foo | lameprog /dev/stdin
=>
=>or whatever for such cases.
Good question. It doesn't come up often, but the best way to get it is to
think in C vs thinking in shell.
>From C, you can create subprocesses and pipes, but when you do it, you
don't have to constrain your dataflow from left to right.
p1 | p2 | p3
has data from from left to right. Now what happens if you want data to
travel differently?
I want p1's arg1 to take its input from p2 piped to p3 and I want p1's
second arg to come from p4.
p1 <(p2 | p3) <( p4)
Easy peasy, japanesy
Here are a few I found online:
sort -k 9 <(ls -l /bin) <(ls -l /usr/bin) <(ls -l /usr/X11R6/bin)
# Lists all the files in the 3 main 'bin' directories, and sorts by filename.
# Note that three (count 'em) distinct commands are fed to 'sort'.
diff <(command1) <(command2) # Gives difference in command output.
tar cf >(bzip2 -c > file.tar.bz2) $directory_name
# Tar up a dir and compress the output (Yeah, I know, just use j opt.)
--
Time flies like the wind. Fruit flies like a banana. Stranger things have .0.
happened but none stranger than this. Does your driver's license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren't a hypothetical question?
steveo at syslang.net
More information about the gnhlug-discuss
mailing list