|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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" |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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 ) |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
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 ) |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#9 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#10 |
|
Messages: n/a
Hébergeur: |
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> |
|
|
|
#11 |
|
Messages: n/a
Hébergeur: |
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 |
|
![]() |
| Outils de la discussion | |
|
|