It is currently Mon, 08 Aug 2022 19:44:14 GMT

Author Message
 Bourne shell "read line"
Abbreviated version of a script:

while read line
   echo "$line"
done < inputfile

shell "read" built-in removes every backslash (\) from the inputfile.
Adding the "-r" to read tells me that it's not an identifier.
Explicitly calling out /usr/bin/read in the "while" statement
spews newlines and crunches indefinitely.

What am I missing?  Besides a clue, I mean ...

Any help, TIA.


 Sat, 14 Feb 2004 23:58:45 GMT   
 Bourne shell "read line"
On 28 Aug 2001, BR wrote:

The -r option is a bash (and ksh?) extension.

As another poster (laura, I think it was) suggested in another thread, you
could filter the file first:

sed 's/\\/\\\\/g inputfile | while read line
do ......

    Chris F.A. Johnson              
    My code (if any) in this post is copyright 2001, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License

 Sun, 15 Feb 2004 00:38:13 GMT   
 Bourne shell "read line"
Chris F.A. Johnson <> wrote:

Thanks, Chris - sorta hacky, but that did it.
Although I had to repeat the sed --

sed -e 's/\\/\\\\/g' -e 's/\\/\\\\/g' inputfile

due to the literal translation of the metachar: \n


 Sun, 15 Feb 2004 04:17:12 GMT   
 Bourne shell "read line"
On 28 Aug 2001 20:17:12 GMT, BR <> wrote:

I'd like to know what you mean by this last statement

echo '\\n' |sed 's/\\/\\\\/g' |{ read a;set |grep '^a='; }

produces a='\n', as one would expect it to..... the problem
you may be having is indicated by the first statement here
since many versions of 'echo' interpret the backslash
character also (so the 'echo' is outputing the string '\n').
if you quadruple and backslashes in your input string
such as to make them doubled after the
read, then single after the 'echo' this will be okay
except that the string you now process in '$line' is
not the exact input string anymore.

also watch out for echo interpretting options '-n' '-e'
because if it happens that '$line' is equal to one of
these then it will be nulled on output, the way
I would workaround this to use an initial nul argument
to effectively end option interpretation without having
any effect on the output;

echo "" "$line"

however, you are still having all backslashes doubled in
the variable. depending on your shell there may be other
ways to output the string that will not interpret any
meta-characters, maybe if you have 'printf';

printf "%s\n" "$line"

otherwise, and more portable is to use 'cat';

cat <<:

See Ya,

: ${L} #

- Show quoted text -

 Mon, 16 Feb 2004 01:42:51 GMT   
 Bourne shell "read line"
Good point - I forgot to check the echo - thought I was
having the same issue with printf, though ...

the input file was/is a perl script using the newlines
within quoted echos/warnings/prints -- but they aren't the
only backslashes ...

I quadrupled the slashes 's/\\/\\\\\\\\/g' before the
read line and echo'd $line (sans quotes) at the tail end.

what can I say? - it worked.  I'll play some more, though.

Thanks for the help.


In article, (laura fairhead) writes:

 Tue, 17 Feb 2004 04:48:08 GMT   
 Bourne shell "read line"
[ This is a repost of an article originally posted on
Wed Aug 29 14:42:57 2001 which got stuck in a news queue ]

"Chris F.A. Johnson" <> writes:

It's required by POSIX, so should be very portable (except to
old pre-POSIX Bourne shells like the OP's).

Geoff Clare               
UniSoft Limited, London, England.

 Tue, 24 Feb 2004 21:22:00 GMT   
   [ 6 post ] 

Similar Threads

1. Q about Bourne shell "read" command

2. Bourne shell: "no stack space" problem

3. Bourne shell "trip test"?

4. Bourne shell equivilant of "source"

5. Bourne Shell Script to "C" Converter

6. "unset" variables under ultrix bourne shell

7. Reading the i:th line from a file in Bourne shell

8. "read" shell command

9. Borne Shell problem with "while read".

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software