bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: problem with builtin 'read' and standard input


From: Enrique Perez-Terron
Subject: Re: problem with builtin 'read' and standard input
Date: Tue, 27 Jul 2004 17:08:21 +0200

On Tue, 2004-07-27 at 11:55, Matthew Walker wrote:
> Hi,
> 
> I'm trying to redirect data to the builtin 'read'.  I've no idea what's 
> going wrong!
> 
> I believe this command
>    echo "hello" | read myvar
> should put "hello" into $myvar.  But $myvar is blank after execution.

Because the "read myvar" is executed in a different process. Try this:

  echo hello | { read myvar; echo $myvar; }

The last echo will output "hello".

You probably tried to do something like

  WRONG> some-process | while read var; do 
                          if some-test; then 
                            found=true
                          fi
                        done
  WRONG> if [ "$found" = true ]; then ... ; fi

but that does not work becase the assignment to "found" happens in a
different subshell. Instead, you can try

  found=`some-process | while read var; do 
                          if some-test; then 
                            echo true
                            break
                          fi 
                        done`
  if [ "$found" = true ]; then ... ; fi

Or, you can try

  < <(some-process) while read var
  do
    if some-test
    then
      found=true
      break
    fi
  done

The space between the two '<' is required.

> 
> If I type
>    read myvar
> and then type
>    hello
> then I get "hello" in $myvar.
> 
> What am I doing wrong?!?  Is this a bug?

I wish there were a way to specify to the shell that one particular of
the processes in a pipeline should be executed by the main process, but
there is not.

Regards,
Enrique





reply via email to

[Prev in Thread] Current Thread [Next in Thread]