read -n option doesn't work right with -u

2007-12-07 Thread b_bashbug
Configuration Information [Automatically generated, do not change]:
Machine: i686
OS: linux-gnu
Compiler: i686-pc-linux-gnu-gcc
Compilation CFLAGS:  -DPROGRAM='bash' -DCONF_HOSTTYPE='i686' 
-DCONF_OSTYPE='linux-gnu' -DCONF_MACHTYPE='i686-pc-linux-gnu' 
-DCONF_VENDOR='pc' -DLOCALEDIR='/usr/share/locale' -DPACKAGE='bash' -DSHELL 
-DHAVE_CONFIG_H   -I.  -I. -I./include -I./lib   -O3 -pipe -march=pentium3 
-fomit-frame-pointer
uname output: Linux newbie 2.6.17-gentoo-r8 #2 SMP Mon Oct 23 23:53:40 EDT 2006 
i686 Pentium III (Coppermine) GenuineIntel GNU/Linux
Machine Type: i686-pc-linux-gnu

Bash Version: 3.2
Patch Level: 17
Release Status: release

Description:
read -u option does not work with -n properly.  In particular, it waits
for a newline if fd 0 is closed.

Repeat-By:
Consider:
exec 3<&0
read -n 1 -u 3
This sequence will read a single character from stdin (the terminal)
without waiting for a newline.  However, consider
exec 3<&0 0<&-
read -n 1 -u 3
This sequence closes fd 0, and bash waits for a newline before reading
the character.
Note that I am working around this as follows:
exec 3<&0 0<&-
read -n 1 <&3
The preceding works, presumably because it's reading from fd 0 again.

Fix:
I would suspect that the -n option needs to pay attention to the -u
flag, and not just assume fd 0.




reading whitespaces and beginning and end of line

2007-12-07 Thread sancho1980

hi

i have a script that reads lines from a file in this manner:

cat infile | while read line
do
echo "$line" >> outfile
done

The problem I have occurs whenever there is a whitespace at the end of a
line in the input file, because this whitespace get automatically truncated
from "$line", and i dont want that..i want the whole line to go into
outfile, no matter if the last characters are whitespace!

thanks very much

martin
-- 
View this message in context: 
http://www.nabble.com/reading-whitespaces-and-beginning-and-end-of-line-tf4961271.html#a14209878
Sent from the Gnu - Bash mailing list archive at Nabble.com.





Re: reading whitespaces and beginning and end of line

2007-12-07 Thread Pierre Gaston
On Dec 7, 2007 12:49 PM, sancho1980 <[EMAIL PROTECTED]> wrote:

> The problem I have occurs whenever there is a whitespace at the end of a
> line in the input file, because this whitespace get automatically truncated
> from "$line", and i dont want that..i want the whole line to go into
> outfile, no matter if the last characters are whitespace!

read the documentation of read.

you can either use
 while read -r ;do echo "$REPLY";done
or
 while IFS='' read -r line;do echo "$line";done




Re: reading whitespaces and beginning and end of line

2007-12-07 Thread Dave Rutherford
On Dec 7, 2007 5:49 AM, sancho1980 <[EMAIL PROTECTED]> wrote:
> i have a script that reads lines from a file in this manner:
>
> cat infile | while read line
> do
> echo "$line" >> outfile
> done
>
> The problem I have occurs whenever there is a whitespace at the end of a
> line in the input file, because this whitespace get automatically truncated
> from "$line", and i dont want that..i want the whole line to go into
> outfile, no matter if the last characters are whitespace!

You do win the coveted "useless use of cat" award, but even
with that I can't reproduce your problem here.  What version
of bash are you using?

$ cat -t -E test1
this $
file  $
contains some   $
extra^I$
whitespace   $
$ while read; do echo "$REPLY" >> test2; done < test1
$ cat test1 | while read; do echo "$REPLY" >> test3; done
$ l test*
-rw-r- 1 dave dave 51 Dec  7 11:19 test1
-rw-r- 1 dave dave 51 Dec  7 11:19 test2
-rw-r- 1 dave dave 51 Dec  7 11:19 test3




Re: reading whitespaces and beginning and end of line

2007-12-07 Thread Chris F.A. Johnson
On 2007-12-07, sancho1980 wrote:
>
> i have a script that reads lines from a file in this manner:
>
> cat infile | while read line

   You don't need cat.

> do
> echo "$line" >> outfile
> done
>
> The problem I have occurs whenever there is a whitespace at the end of a
> line in the input file, because this whitespace get automatically truncated
> from "$line", and i dont want that..i want the whole line to go into
> outfile, no matter if the last characters are whitespace!

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

-- 
   Chris F.A. Johnson, webmaster 
   ===
   Author:
   Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
.