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:
John Baxter <[log in to unmask]>
Reply To:
Macintosh Scripting Systems <[log in to unmask]>
Date:
Fri, 3 Aug 2007 18:01:39 -0700
Content-Type:
text/plain
Parts/Attachments:
text/plain (63 lines)
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.  Note the complete lack of cat.

   --John

ATOM RSS1 RSS2