PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > export variable from a subshell
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

export variable from a subshell

Réponse
 
LinkBack Outils de la discussion
Vieux 01/11/2006, 23h39   #1
Daniel Hagen
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut export variable from a subshell

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










  Réponse avec citation
Vieux 02/11/2006, 02h45   #2
Geoff Gigg
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: export variable from a subshell

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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 02h40.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,08885 seconds with 10 queries