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 > variable outside loop equals 0
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

variable outside loop equals 0

Réponse
 
LinkBack Outils de la discussion
Vieux 24/07/2007, 13h09   #1
joe
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut variable outside loop equals 0

Hello I am using the script below mitems becomes 0 outside de loop.
Does anyone know how to fix this? thanks.

#!/bin/bash

MITEMS="0"

cat numbers|while read line; do
if (("$line" > 40)); then
##MITEMS=$[$MITEMS + 1]
(( MITEMS++ ));
fi

echo "$MITEMS"

  Réponse avec citation
Vieux 24/07/2007, 14h46   #2
Kenny McCormack
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: variable outside loop equals 0

In article <1185278960.579635.302670@q75g2000hsh.googlegroups .com>,
joe <jcharth@gmail.com> wrote:
>Hello I am using the script below mitems becomes 0 outside de loop.
>Does anyone know how to fix this? thanks.
>
>#!/bin/bash
>
>MITEMS="0"
>
>cat numbers|while read line; do
>if (("$line" > 40)); then
>##MITEMS=$[$MITEMS + 1]
>(( MITEMS++ ));
>fi
>
>echo "$MITEMS"
>


As others have pointed out, this is a FAQ, and the real answer is that
there is always a better way to do things than in shell.

But, the bottom line is that this is a UUOC.
You should never do "cat file | ...". Change it to:

while read line ... done < file

and it won't run in a subshell.

  Réponse avec citation
Vieux 24/07/2007, 14h46   #3
Kenny McCormack
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: variable outside loop equals 0

In article <1185278960.579635.302670@q75g2000hsh.googlegroups .com>,
joe <jcharth@gmail.com> wrote:
>Hello I am using the script below mitems becomes 0 outside de loop.
>Does anyone know how to fix this? thanks.
>
>#!/bin/bash
>
>MITEMS="0"
>
>cat numbers|while read line; do
>if (("$line" > 40)); then
>##MITEMS=$[$MITEMS + 1]
>(( MITEMS++ ));
>fi
>
>echo "$MITEMS"
>


As others have pointed out, this is a FAQ, and the real answer is that
there is always a better way to do things than in shell.

But, the bottom line is that this is a UUOC.
You should never do "cat file | ...". Change it to:

while read line ... done < file

and it won't run in a subshell.

  Réponse avec citation
Vieux 24/07/2007, 16h37   #4
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: variable outside loop equals 0

joe wrote:
> Hello I am using the script below mitems becomes 0 outside de loop.
> Does anyone know how to fix this? thanks.
>
> #!/bin/bash
>
> MITEMS="0"
>
> cat numbers|while read line; do
> if (("$line" > 40)); then
> ##MITEMS=$[$MITEMS + 1]
> (( MITEMS++ ));
> fi
>
> echo "$MITEMS"
>


Don't write the loop at all. Depending what you want to print if less
than 41 lines, this may be all you need:

echo $(( $(wc -l < file) - 40 ))

If you're trying to do something other than count the number of lines
over 40, again don't write the loop but tell us what you're trying to do
if you'd like with the right solution.

Ed.
  Réponse avec citation
Vieux 24/07/2007, 16h37   #5
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: variable outside loop equals 0

joe wrote:
> Hello I am using the script below mitems becomes 0 outside de loop.
> Does anyone know how to fix this? thanks.
>
> #!/bin/bash
>
> MITEMS="0"
>
> cat numbers|while read line; do
> if (("$line" > 40)); then
> ##MITEMS=$[$MITEMS + 1]
> (( MITEMS++ ));
> fi
>
> echo "$MITEMS"
>


Don't write the loop at all. Depending what you want to print if less
than 41 lines, this may be all you need:

echo $(( $(wc -l < file) - 40 ))

If you're trying to do something other than count the number of lines
over 40, again don't write the loop but tell us what you're trying to do
if you'd like with the right solution.

Ed.
  Réponse avec citation
Vieux 24/07/2007, 17h29   #6
Icarus Sparry
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: variable outside loop equals 0

On Tue, 24 Jul 2007 10:37:24 -0500, Ed Morton wrote:

> joe wrote:
>> Hello I am using the script below mitems becomes 0 outside de loop.
>> Does anyone know how to fix this? thanks.
>>
>> #!/bin/bash
>>
>> MITEMS="0"
>>
>> cat numbers|while read line; do
>> if (("$line" > 40)); then
>> ##MITEMS=$[$MITEMS + 1]
>> (( MITEMS++ ));
>> fi
>>
>> echo "$MITEMS"
>>
>>

> Don't write the loop at all. Depending what you want to print if less
> than 41 lines, this may be all you need:
>
> echo $(( $(wc -l < file) - 40 ))
>
> If you're trying to do something other than count the number of lines
> over 40, again don't write the loop but tell us what you're trying to do
> if you'd like with the right solution.
>
> Ed.


I think he wants to count the number of lines where the value on the line
is greater than 40.

MITEMST=$( awk '$1>40 { c++ ; } END {print c+0}' numbers )
  Réponse avec citation
Vieux 24/07/2007, 17h29   #7
Icarus Sparry
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: variable outside loop equals 0

On Tue, 24 Jul 2007 10:37:24 -0500, Ed Morton wrote:

> joe wrote:
>> Hello I am using the script below mitems becomes 0 outside de loop.
>> Does anyone know how to fix this? thanks.
>>
>> #!/bin/bash
>>
>> MITEMS="0"
>>
>> cat numbers|while read line; do
>> if (("$line" > 40)); then
>> ##MITEMS=$[$MITEMS + 1]
>> (( MITEMS++ ));
>> fi
>>
>> echo "$MITEMS"
>>
>>

> Don't write the loop at all. Depending what you want to print if less
> than 41 lines, this may be all you need:
>
> echo $(( $(wc -l < file) - 40 ))
>
> If you're trying to do something other than count the number of lines
> over 40, again don't write the loop but tell us what you're trying to do
> if you'd like with the right solution.
>
> Ed.


I think he wants to count the number of lines where the value on the line
is greater than 40.

MITEMST=$( awk '$1>40 { c++ ; } END {print c+0}' numbers )
  Réponse avec citation
Vieux 24/07/2007, 17h39   #8
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: variable outside loop equals 0

Icarus Sparry wrote:
> On Tue, 24 Jul 2007 10:37:24 -0500, Ed Morton wrote:
>
>
>>joe wrote:
>>
>>>Hello I am using the script below mitems becomes 0 outside de loop.
>>>Does anyone know how to fix this? thanks.
>>>
>>>#!/bin/bash
>>>
>>>MITEMS="0"
>>>
>>>cat numbers|while read line; do
>>>if (("$line" > 40)); then
>>>##MITEMS=$[$MITEMS + 1]
>>>(( MITEMS++ ));
>>>fi
>>>
>>>echo "$MITEMS"
>>>
>>>

>>
>>Don't write the loop at all. Depending what you want to print if less
>>than 41 lines, this may be all you need:
>>
>>echo $(( $(wc -l < file) - 40 ))
>>
>>If you're trying to do something other than count the number of lines
>>over 40, again don't write the loop but tell us what you're trying to do
>>if you'd like with the right solution.
>>
>> Ed.

>
>
> I think he wants to count the number of lines where the value on the line
> is greater than 40.
>
> MITEMST=$( awk '$1>40 { c++ ; } END {print c+0}' numbers )


Yeah, you're right. Don't know what I was thinking. Thanks.

Ed.
  Réponse avec citation
Vieux 24/07/2007, 17h39   #9
Ed Morton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: variable outside loop equals 0

Icarus Sparry wrote:
> On Tue, 24 Jul 2007 10:37:24 -0500, Ed Morton wrote:
>
>
>>joe wrote:
>>
>>>Hello I am using the script below mitems becomes 0 outside de loop.
>>>Does anyone know how to fix this? thanks.
>>>
>>>#!/bin/bash
>>>
>>>MITEMS="0"
>>>
>>>cat numbers|while read line; do
>>>if (("$line" > 40)); then
>>>##MITEMS=$[$MITEMS + 1]
>>>(( MITEMS++ ));
>>>fi
>>>
>>>echo "$MITEMS"
>>>
>>>

>>
>>Don't write the loop at all. Depending what you want to print if less
>>than 41 lines, this may be all you need:
>>
>>echo $(( $(wc -l < file) - 40 ))
>>
>>If you're trying to do something other than count the number of lines
>>over 40, again don't write the loop but tell us what you're trying to do
>>if you'd like with the right solution.
>>
>> Ed.

>
>
> I think he wants to count the number of lines where the value on the line
> is greater than 40.
>
> MITEMST=$( awk '$1>40 { c++ ; } END {print c+0}' numbers )


Yeah, you're right. Don't know what I was thinking. Thanks.

Ed.
  Réponse avec citation
Vieux 24/07/2007, 23h37   #10
Sven Mascheck
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: variable outside loop equals 0

Kenny McCormack wrote:

> You should never do "cat file | ...". Change it to:


No, actually there are some more or less special examples
where this is not true (search for "useful use of cat" or
see <e909laUpp4L1@news.in-ulm.de>). But it can even be
just convenient to have "cat file" replacing a command
here (to be swapped while debugging e.g.).


> while read line ... done < file
>
> and it won't run in a subshell.


No, this is not true for traditional Bourne shells.
--
<http://www.in-ulm.de/~mascheck/various/uuoc/>
<http://www.in-ulm.de/~mascheck/bourne/common.html>
  Réponse avec citation
Vieux 25/07/2007, 08h46   #11
Daniel Rock
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: variable outside loop equals 0

Sven Mascheck <cus.r.mascheck@spamgourmet.com> wrote:
> Kenny McCormack wrote:
>> while read line ... done < file
>>
>> and it won't run in a subshell.

>
> No, this is not true for traditional Bourne shells.


exec 3<&0 0< file
while read line; do
...
done
exec 0<&3 3<&-

(in this case I prefer to name file descriptor 0 for readability)

This construct will first save stdin to file descriptor 3, then redirect
fd 0 from "file".

After the while loop the saved stdin will be mapped back to fd 0, and fd 3
won't be needed any more, therefor closed.

If fd 3 is already in use by your script, you have to use another digit.

--
Daniel
  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 09h27.


É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,27733 seconds with 19 queries