bug-bash
[Top][All Lists]
Advanced

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

Re: read output of process into a variable


From: Michael Potter
Subject: Re: read output of process into a variable
Date: Wed, 30 Jan 2008 08:54:35 -0600

It is not a bug in bash.  it is just how it works.  the while loop
creates a subshell and changes to the variables are not visable
outside of the subshell.  if you put the while loop first, then it
will not create the subshell.

do this:

  result=""
  while read line; do
    extracteddata=`echo "$line" | sed -e 's/X/Y/'`
    result="$result $extracteddata"
  done < <(/usr/bin/output_generator)
  /usr/bin/another_tool "$result"


the <() is syntax for a named pipes.  it makes a command look like a file.

Be aware that this may leave files in your /tmp directory.

BTW: I would use $() syntax instead of the backtic syntax; just easier to see.
-- 
potter

On 30 Jan 2008 11:21:34 GMT, Stefan Palme <kleiner@hora-obscura.de> wrote:
> Hi,
> don't know if this is the right newsgroup, but it's the only one
> I can find with "bash" in its name :-)
>
> I want to do something like this:
>
>   result=""
>   /usr/bin/output_generator | while read line; do
>     extracteddata=`echo "$line" | sed -e 's/X/Y/'`
>     result="$result $extracteddata"
>   done
>   /usr/bin/another_tool "$result"
>
> In the last line is "result" as empty as at the start of the
> whole thing - I guess because the inner "while" loop is executed
> in a subshell, so that changing the value of "result" in this
> loop does not affect the "outer result".
>
> How can I solve this? I have some very ugly solutions, but
> I guess there must be something "nice" :-)
>
> (using bash-3.2.17(1)-release)
>
> Thanks and regards
> -stefan-
>
>
>




reply via email to

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