On Aug 3, 2007, at 9:01 PM, John Baxter wrote:
> On Aug 3, 2007, at 2:34 PM, David Livesay wrote:
>
>> I understand that, but how do you get cat to put quotes around
>> each line?
>
> Sorry, Dave, I was just trying to show the interaction of the shell
> and the echo command. (Also, I didn't have time earlier to drag
> the book out.)
>
>
> Here is one paradigm for reading lines from a file. (Courtesy of
> page 174 of "Learning the bash Shell", second edition.) (There are
> several others shown before this one, along with a suggestion not
> to do it this way for "large" files [over a few hundred lines,
> which probably applies to your logs] for performance reasons. But
> since you're doing a one-time task, you really don't care about
> performance: "you paid for those cycles, you might as well use
> them".)
>
>
> The script:
> $cat reader.sh
> #!/bin/bash
>
> {
> while read line; do
> echo "$line"
> done
> } < test.txt
>
> The test file:
> $cat test.txt
> one
> two
> three four
> five six seven eight
> nine
>
> Running the script (I did make it executable)
> $./reader.sh
> one
> two
> three four
> five six seven eight
> nine
>
>
> If I remove the quotes around $line in the echo "$line" command,
> the result becomes what you don't want:
> $./reader.sh
> one
> two
> three four
> five six seven eight
> nine
>
>
>
> The script creates a nameless function ("block" in C terms), then
> executes the function redirecting standard input to come from the
> file.
This is great, John. Thanks. I'm definitely going to have to pick up
that book.
> Note the complete lack of cat.
That's preferable to a complete lack of...
I'm not going to say it. ;-)
|