Pipes and subshells w/bash

Pipes and subshells w/bash

Post by Jehs » Fri, 07 Jul 2000 04:00:00



I have something that looks like this:

total=0
grep -E "pattern" file | while read line; do
        size=$(echo $line | cut -f3 -d ' ')
    total=$(($total + $size))
done
echo $total

Needless to say, I get a total of 0 in the end, because the whole while
loop executes in a subshell.
Is there any way I can parse this file and add up the 3rd field of the
select lines, and get a total that I can use in the rest of the script?

Thanks,
Moshe

--




 
 
 

Pipes and subshells w/bash

Post by Charles Dem » Fri, 07 Jul 2000 04:00:00




>I have something that looks like this:

>total=0
>grep -E "pattern" file | while read line; do
>    size=$(echo $line | cut -f3 -d ' ')
>    total=$(($total + $size))
>done
>echo $total

>Needless to say, I get a total of 0 in the end, because the whole
>while loop executes in a subshell.

>Is there any way I can parse this file and add up the 3rd field of the
>select lines, and get a total that I can use in the rest of the script?

Sure, you could do something like this:

total=0
grep -E "pattern" file | while read line; do
        size=$(echo $line | cut -f3 -d ' ')
    total=$(($total + $size))
echo $total > mytotal
done
total=`cat mytotal`
rm mytotal
echo $total

but I suspect it's easier to do the whole thing like this:

total=0
total=`grep -E "pattern" file |
awk -v sum=$total '{sum+=$3} END {print sum}'`
echo $total

or the simpler (which assumes the prior total is zero):

total=`grep -E "pattern" file | awk '{sum+=$3} END {print sum}'`
echo $total

Chuck Demas
Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.


 
 
 

Pipes and subshells w/bash

Post by Kurt J. Lanz » Fri, 07 Jul 2000 04:00:00



> I have something that looks like this:

> total=0
> grep -E "pattern" file | while read line; do
>         size=$(echo $line | cut -f3 -d ' ')
>     total=$(($total + $size))
> done
> echo $total

> Needless to say, I get a total of 0 in the end, because the whole while
> loop executes in a subshell.
> Is there any way I can parse this file and add up the 3rd field of the
> select lines, and get a total that I can use in the rest of the script?

awk ' $0 ~ /pattern/ { tot += $3 } END { print tot } '
file

or something close. "man awk" for more details.

 
 
 

1. Bashing bash (Was Re: bash or user error with set -e and subshells)

Hi there,

This thread was initially about whether it is a bug or a feature, that
bash pedanticly sticks to the POSIX-sufficient requirement that 'set -e'
exit only after getting a nonzero status from a "simple command", while
the original Bourne makes a very useful generalization to "any child"
(including subshells).

I'm still interested in the answer: could some kind person please
address this issue specifically, without digressing to the (otherwise
exciting) millions of ways of avoiding a subshell, or good/bad reasons
to use 'set -e' ?...

What's the rationale after all ? The generalization is trivial to
implement, while the bash behavior has already consumed many
person-months in debugging (as witnessed by groups.google.com) !

Please...

TIA,

-Alex

2. Help - le0: No carrier?

3. Subshells with bash piping into tee

4. Trouble with Mosaic 2.2 binaries

5. BASH BASH BASH BASH BASH BASH BASH BASH BASH BASH

6. help: how to flush a socket...?

7. variable scope: is a pipe a subshell ?

8. Q: Good SCO Vector Drawing Program

9. `Nested (`Nested Subshells`) Subshells`

10. tcsh - to subshell or not to subshell

11. Getting Exit Status from subshell and pipe

12. ksh - no "read" from pipe, but not common "subshell" issue

13. getting status from a subshell command in a pipe-line