|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Hello
i've tried to export a variable from within a 'command| while... ' loop. The only way i figured out to do this is using a fifo (i've decided to use a fifo instead of generating tempfiles via mktemp) . The Script would look like this #!/bin/bash sock=/tmp/$RANDOM if [ ! -S $sock ];then /usr/bin/mkfifo $sock fi var=1 echo "Value before $var" /bin/cat `pwd`/test.txt | while read line;do if [ "`echo $line|/bin/grep XYZ|wc -l`" -ge 1 ];then ((var++)) && echo $var > $sock & fi done var=`more < $sock` echo "Value afer loop $var" rm $sock Is this the only way to the solution or are there other "workarounds" known ? Thanks in advice Daniel |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
Daniel Hagen wrote:
> Hello > > > i've tried to export a variable from within a 'command| while... ' > loop. The only way i figured out to do this is using a fifo (i've decided > to use a fifo instead of generating tempfiles via mktemp) . > > The Script would look like this > > #!/bin/bash > > sock=/tmp/$RANDOM > > if [ ! -S $sock ];then > /usr/bin/mkfifo $sock > fi > > var=1 > echo "Value before $var" > > /bin/cat `pwd`/test.txt | while read line;do > if [ "`echo $line|/bin/grep XYZ|wc -l`" -ge 1 ];then > ((var++)) && echo $var > $sock & > fi > done > > var=`more < $sock` > echo "Value afer loop $var" > rm $sock > > Is this the only way to the solution or are there other "workarounds" > known ? > > Thanks in advice > Daniel Could you not use process substitution, which has the same effect but avoids the use of the sub-shell? The while would take the form of: while read line do .... done < <(/bin/cat/ $(pwd)/test.txt) Note that spacing between < < and no space between <( is important on the done. See http://tldp.org/LDP/abs/html/process-sub.html for more. Geoff |
|
![]() |
| Outils de la discussion | |
|
|