Re: probleme with read command in a loop

From: laura fairhead (LoveMrsRitchie@madonnaweb.com)
Date: 04/12/03


From: LoveMrsRitchie@madonnaweb.com (laura fairhead)
Date: Sat, 12 Apr 2003 07:31:43 GMT

On Fri, 11 Apr 2003 13:24:58 +0000, Philippe Rousselot <usergroup@rousselot.org>
wrote:

>Hi,
>
>I hope this this the right place to ask and sorry if not.
>
>I made a script to remove accentuated characters from a series of file name
>
>~~~~~~~~~~~~~~~

>#!/bin/bash

here add;

exec 3<&0

>find ./test_recode -name "*" | while read ff
>do
> f=$ff ;
> export f=`echo $f | tr "àäâçéèêëîïôöùüû" "aaaceeeeiioouuu"` ;
> export f=`echo $f | tr "ÂÀÄÇÉÈÊËÎÏÔÖÙÜÛ " "AAACEEEEIIOOUUU_"` ;
># export f=`echo $f | tr '[:upper:]' '[:lower:]'` ;
> if [ "$ff" != "$f" ]
> then
> echo "f avant = $ff" ;
> echo "f après = $f" ;
> echo ""
> echo -n "remplacer le fichier (y/n)? " ;
>
> read var1

replace this line with;

read var1 <&3

>
> if [ "$var1" = 'y' ] ; then
> mv "$ff" "$f"
> fi
> fi
>
>done

and here add ;

exec 3<&-

>
>Why doesn't the script stop at the read command ?
>

Because the standard input (file descriptor 0) is redirected within
the loop since your loop is at the end of a pipe-line. The default
file descriptor for 'read' to get data input is the standard input
on file descriptor 0, which is usually the terminal but here it
is the output of the find command from the previous pipe.

[ You may consider also why should you expect the 'read' command on that
line to behave any differently from the one at the start of the
while loop? Of course it has the same action - and reads another line
from the 'find' command ]

One solution is to duplicate the terminal device file descriptor outside
the pipeline and put it into fd.3 .Then use fd.3 inside the loop when you
want to read from the terminal and finally close it after the pipeline
ends - I have indicated those modifications above for you :)

byefornow
laura

>Thank you in advance
>
>Philippe
>

-- 
   "The Bush-Blair war can be called a lot of things, but it cannot
   be called a war to free Iraqis. A better name is the `War of Lies,'
   because it is through lies that they think they can fool all the
   people all the time,"
"Freedom To Kill Iraqis" - Taher Al Adwan [ Jordanian Newspaper Columnist ]


Relevant Pages

  • Re: Testing for loops
    ... I tried this while loop ... > path to a binary than to just specifiy the executable name. ... Try timing a script written ... command in vi, or the abbrev mode in emacs. ...
    (comp.unix.shell)
  • Re: Strange errors
    ... script, or at another point in the loop. ... you can do classin the command window at that ... why does the same exact code not ...
    (comp.soft-sys.matlab)
  • Re: unix pipes to perl scripts
    ... > the loop accepts the piped files but it also interferes with my STDIN! ... The STDIN of your pp script is redirected from the keyboard to the ... STDOUT of the ls command. ...
    (comp.lang.perl.misc)
  • Re: searching synchronously or asynchronously
    ... computer and immediately return to the loop, ... I have the below script. ... Is there a way to run the script, connect to all computers in the command ... If fName NetPath and fName LocalPath and _ ...
    (microsoft.public.scripting.wsh)
  • Re: bash question...
    ... Do you want to be able to input the command, or do you want a script ... If the command is in your script, you can do what Chris has. ... descriptor for redirecting the file input. ... redirecting the file to file descriptor #3, ...
    (comp.unix.shell)