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 > bash while read line and variable scope?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

bash while read line and variable scope?

Réponse
 
LinkBack Outils de la discussion
Vieux 16/05/2007, 09h09   #1
Roc Zhou
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut bash while read line and variable scope?

Look at these 2 similar scripts:
(1)
echo -e "aaa\nbbb\nccc\nddd" >tmp
value=0;
while read line; do
value=`expr $value + 1`
echo $value
done <tmp
rm -f tmp
echo "at last: $value"

result:
1
2
3
4
at last: 4

(2)
value=0
echo -e "aaa\nbbb\nccc\nddd" | while read line; do
value=`expr $value + 1`
echo $value
done
echo "at last: $value"

result:
1
2
3
4
at last: 0

I don't want to add a 'tmp' file, is there any other way? And I also
want to know what makes the differences.

Thank you

  Réponse avec citation
Vieux 16/05/2007, 10h41   #2
Kenan Kalajdzic
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: bash while read line and variable scope?

Roc Zhou <chowroc.z@gmail.com> wrote:
> Look at these 2 similar scripts:
> (1)
> echo -e "aaa\nbbb\nccc\nddd" >tmp
> value=0;
> while read line; do
> value=`expr $value + 1`
> echo $value
> done <tmp
> rm -f tmp
> echo "at last: $value"
>
> result:
> 1
> 2
> 3
> 4
> at last: 4
>
> (2)
> value=0
> echo -e "aaa\nbbb\nccc\nddd" | while read line; do
> value=`expr $value + 1`
> echo $value
> done
> echo "at last: $value"
>
> result:
> 1
> 2
> 3
> 4
> at last: 0
>
> I don't want to add a 'tmp' file, is there any other way? And I also
> want to know what makes the differences.


Try this script and see if it gives you any clues:

echo "---" >tmp
while read line; do
ps aux | grep "$0" | grep -v grep
done <tmp
rm -f tmp

echo "---"
echo line | while read line; do
ps aux | grep "$0" | grep -v grep
done

--
Kenan Kalajdzic
  Réponse avec citation
Vieux 16/05/2007, 17h08   #3
Chris F.A. Johnson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: bash while read line and variable scope?

On 2007-05-16, Roc Zhou wrote:
> Look at these 2 similar scripts:
> (1)
> echo -e "aaa\nbbb\nccc\nddd" >tmp
> value=0;
> while read line; do
> value=`expr $value + 1`
> echo $value
> done <tmp
> rm -f tmp
> echo "at last: $value"
>
> result:
> 1
> 2
> 3
> 4
> at last: 4
>
> (2)
> value=0
> echo -e "aaa\nbbb\nccc\nddd" | while read line; do
> value=`expr $value + 1`
> echo $value
> done
> echo "at last: $value"
>
> result:
> 1
> 2
> 3
> 4
> at last: 0
>
> I don't want to add a 'tmp' file, is there any other way? And I also
> want to know what makes the differences.


See the FAQ: <http://cfaj.freeshell.org/shell/cus-faq.html#JJ>

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
  Réponse avec citation
Vieux 17/05/2007, 04h51   #4
Roc Zhou
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: bash while read line and variable scope?

I got it, Thank you very much.

On 5ÔÂ17ÈÕ, ÉÏÎç12ʱ08·Ö, "Chris F.A. Johnson" <cfajohn...@gmail.com> wrote:
> On 2007-05-16, Roc Zhou wrote:
>
>
>
> > Look at these 2 similar scripts:
> > (1)
> > echo -e "aaa\nbbb\nccc\nddd" >tmp
> > value=0;
> > while read line; do
> > value=`expr $value + 1`
> > echo $value
> > done <tmp
> > rm -f tmp
> > echo "at last: $value"

>
> > result:
> > 1
> > 2
> > 3
> > 4
> > at last: 4

>
> > (2)
> > value=0
> > echo -e "aaa\nbbb\nccc\nddd" | while read line; do
> > value=`expr $value + 1`
> > echo $value
> > done
> > echo "at last: $value"

>
> > result:
> > 1
> > 2
> > 3
> > 4
> > at last: 0

>
> > I don't want to add a 'tmp' file, is there any other way? And I also
> > want to know what makes the differences.

>
> See the FAQ: <http://cfaj.freeshell.org/shell/cus-faq.html#JJ>
>
> --
> Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/>
> Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
> ===== My code in this post, if any, assumes the POSIX locale
> ===== and is released under the GNU General Public Licence



  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 12h29.


Édité par : vBulletin® version 3.7.2
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
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,11638 seconds with 12 queries