MACSCRPT Archives

August 2007

MACSCRPT@LISTSERV.DARTMOUTH.EDU

Options: Use Monospaced Font
Show Text Part by Default
Show All Mail Headers

Message: [<< First] [< Prev] [Next >] [Last >>]
Topic: [<< First] [< Prev] [Next >] [Last >>]
Author: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
Subject:
From:
David Livesay <[log in to unmask]>
Reply To:
Macintosh Scripting Systems <[log in to unmask]>
Date:
Fri, 3 Aug 2007 21:13:40 -0400
Content-Type:
text/plain
Parts/Attachments:
text/plain (73 lines)
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. ;-)

ATOM RSS1 RSS2