Unix questions - Read line at a time and send to stdout

This is Interesting: Free IT Magazines  
Home > Archive > Unix questions > August 2006 > Read line at a time and send to stdout





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Read line at a time and send to stdout
Dave (from the UK)

2006-08-06, 7:18 pm

Why does the following script:

-------------------------------
#!/bin/sh
# This is a script called 'copy2out'
if [ $# != 1 ]
then
echo "Usage: $0 file"
exit 1
fi

# Open the file for reading and put on stdout
while read f
do
echo $f
done < $1
exit 0
----------------------------------


*NOT* copy the contents of a file to stdout?

It almost does it, but someones fails. An example of where it fails is
on this 3 kB or so text file

http://www.althorne.org/testfile.txt

If I call the script 'copy2out' then:

% copy2out testfile.txt > outfile
% cmp testfile.txt outfile
testfile.txt outfile differ: char 446, line 18
% diff testfile.txt outfile
18c18
< Not quite the exchange variaton, but pretty much like it. } 4.d4 Bd6
{Bishops
---
> Not quite the exchange variaton, but pretty much like it. } 4.d4 Bd6

{Bishops
20,24c20,24
< are developed, but neither knight, which is unusual. Was this the
right thing


and so on. So the copy is not happening correctly. Any idea why, or a
better way to do this?


--
Dave (from the UK)

Please note my email address changes periodically to avoid spam.
It is always of the form: month-year@southminster-branch-line.org.uk
Hitting reply will work for a few months only - later set it manually.

http://witm.sourceforge.net/ (Web based Mathematica front end)
Barry Margolin

2006-08-07, 1:21 am

In article <44d681cc@212.67.96.135>,
"Dave (from the UK)"
<see-my-signature@southminster-branch-line.org.uk> wrote:

> Why does the following script:
>
> -------------------------------
> #!/bin/sh
> # This is a script called 'copy2out'
> if [ $# != 1 ]
> then
> echo "Usage: $0 file"
> exit 1
> fi
>
> # Open the file for reading and put on stdout
> while read f
> do
> echo $f
> done < $1
> exit 0
> ----------------------------------
>
>
> *NOT* copy the contents of a file to stdout?
>
> It almost does it, but someones fails. An example of where it fails is
> on this 3 kB or so text file


You need to quote the argument to echo:

echo "$f"

Otherwise, the shell performs word splitting and wildcard expansion on
the variable's value.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
Dave (from the UK)

2006-08-07, 1:21 am

Barry Margolin wrote:
> In article <44d681cc@212.67.96.135>,
> "Dave (from the UK)"
> <see-my-signature@southminster-branch-line.org.uk> wrote:
>


> You need to quote the argument to echo:
>
> echo "$f"
>
> Otherwise, the shell performs word splitting and wildcard expansion on
> the variable's value.
>


I had already tried that, but it does not work.



teal /export/home/drkirkby/chess % cat copy2out
#!/bin/sh

if [ $# != 1 ]
then
echo "Usage: $0 file"
exit 1
fi


# Open the file for reading.
while read "f"
do
echo "$f"
done < $1
exit 0
teal /export/home/drkirkby/chess % copy2out 2k.pgn > g
teal /export/home/drkirkby/chess % cmp 2k.pgn g
2k.pgn g differ: char 11246, line 336



--
Dave (from the UK)

Please note my email address changes periodically to avoid spam.
It is always of the form: month-year@southminster-branch-line.org.uk
Hitting reply will work for a few months only - later set it manually.

http://witm.sourceforge.net/ (Web based Mathematica front end)
Chris F.A. Johnson

2006-08-07, 1:21 am

On 2006-08-06, Dave (from the UK) wrote:
> Why does the following script:
>
> -------------------------------
> #!/bin/sh
> # This is a script called 'copy2out'
> if [ $# != 1 ]
> then
> echo "Usage: $0 file"
> exit 1
> fi
>
> # Open the file for reading and put on stdout
> while read f
> do
> echo $f
> done < $1
> exit 0
> ----------------------------------
>
>
> *NOT* copy the contents of a file to stdout?
>
> It almost does it, but someones fails. An example of where it fails is
> on this 3 kB or so text file
>
> http://www.althorne.org/testfile.txt
>
> If I call the script 'copy2out' then:
>
>% copy2out testfile.txt > outfile
>% cmp testfile.txt outfile
> testfile.txt outfile differ: char 446, line 18
>% diff testfile.txt outfile
> 18c18
>< Not quite the exchange variaton, but pretty much like it. } 4.d4 Bd6
> {Bishops
> ---
> {Bishops
> 20,24c20,24
>< are developed, but neither knight, which is unusual. Was this the
> right thing
>
>
> and so on. So the copy is not happening correctly. Any idea why, or a
> better way to do this?


Your script will delete leading and trailing spaces and remove
multiple spaces between words. To prevent the former, set IFS to
an empty string when reading the file; for the latter, quote the
variable when you use it (and printf is more reliable than echo).

while IFS= read -r f
do
printf "%s\n" "$f"
done < "$1"


--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com