6/27 Stupid UNIX question: In a shell program, how do I read in
standard input? I want the program to take standard input
and manipulate it in some way and then output the result to
a file. Once I have the program written, I know how to invoke
it, but how do I, within the program, tell it to work with the
standard input?
\_ Read from fd 1:
while read LINE ; do echo $LINE ; done <&1
\_ If you just want to pass stdin through various external commands,
it's easy; just run them, and the first one will consume stdin if
it needs it; e.g.:
#!/bin/sh
awk '{print $2}' | sed 's/a/b/g' > file
will "just work". If you want to perform actual line by line
shell script programming, you'll have to do like the previous post
says. But really, if you're doing actual custom programming of
this type (i.e. not lots of external program invocations), you
almost certainly are better off using perl or <insert favorite
text munging language here> --dbushong |