Re: [SLUG] Scripting Question

From: Paul Braman (aeon@tampabay.rr.com)
Date: Wed Nov 28 2001 - 20:22:30 EST


On Wed, 28 Nov 2001, Steven Johnson wrote:

> If this has not already been covered, why not:
>
>
> #!/bin/sh
>
> for LINE in `cat ${FILE}`
> do
> echo ${LINE}
> done

It's already been covered, but just to point out the mistake in this
approach:

The problem here is when you have a line in the file that has imbedded
spaces and tabs. While this approach is the simplest for reading the file
and processing the results you need to combine it with setting the IFS
variable. The IFS variable specifies the inter-field seperator. By
default it's set to <space>, <tab>, and <newline>. When you read in a
file that has a line with spaces and tabs on it you will actually get each
individual "token" instead of the whole line in one shot.

To fix your code, simply add the following line above the for-loop:

==============
IFS='
'
==============

Note, the first single quote is followed directly by a newline. No
extraneous spaces or tabs before the end of the line or they become part
of the list.

Hope this helps.

Paul Braman
aeon@tampabay.rr.com



This archive was generated by hypermail 2.1.3 : Fri Aug 01 2014 - 18:57:26 EDT