[lug] Some help with bash, please

Kenneth D Weinert kenw at quarter-flash.com
Wed Feb 20 09:55:28 MST 2008


As is usual with such things, after sending out my plea for help I
sorted out a way to get it to work by eliminating the inner loop.

However, your way is much more efficient - mine took 1:31 and yours took
0:09 (M:SS).

Thanks for not only the way to do it, but the "behind the scenes" look
at how it came together.

It was also a check to ensure that I did it right the first time - our
counts agreed :)


On Wed, 2008-02-20 at 09:38 -0700, Jeffrey Haemer wrote:
> Ken,
> 
> You're just trying to be too clever.  Here's an easier way, done by,
> "recall the line and add something."  Programming by accretion. :-)
> 
> $ cat /tmp/foo
> fstab
> passwd
> group
> $ for i in $(< /tmp/foo); do echo /etc/$i; done   # Want to ensure
> they exist? s/echo/ls/
> /etc/fstab
> /etc/passwd
> /etc/group
> $ wc -l $(for i in $(< /tmp/foo); do echo /etc/$i; done)
>   39 /etc/fstab
>   81 /etc/group
>   54 /etc/passwd
>  174 total
> $ wc -l $(for i in $(< /tmp/foo); do echo /etc/$i; done) | perl -lane
> 'print if s/ total//'
>  174
> $ total=$(wc -l $(for i in $(< /tmp/foo); do echo /etc/$i; done) |
> perl -lane 'print if s/ total//')
> $ echo $total
> 174
> 
> 
> Have another cup of coffee.  I'm going to. :-)
> 
> On Wed, Feb 20, 2008 at 9:17 AM, Kenneth D Weinert
> <kenw at quarter-flash.com> wrote:
>         OK, I'm feeling really dumb right now as this just isn't
>         working for me.
>         Here's the script I'm running:
>         
>         #!/bin/bash
>         
>         declare -xi total=0
>         declare -xi count
>         
>         cat /tmp/orders | while read file;do
>                wc -l /dgorder/$file;
>         done | while read count file;do
>                total=$((total + count))
>         done
>         
>         echo "Number of Orders: $(wc -l /tmp/orders | tr -s ' '|cut
>         -d' ' -f2)"
>         echo "Total Documents:  $total"
>         
>         /tmp/orders is a file that has a filename, one per line
>         
>         Each /dgorder/$file has a number of records in it and I'm
>         trying to
>         count the total number of records in all the files.
>         
>         I think the double loop is what's killing me as the $total
>         variable
>         *always* ends up being zero.
>         
>         Any thoughts, hints, pointers, other scripting languages are
>         more than
>         welcome.  Since all the files are in the same directory I can
>         fix the
>         main input file to have complete paths instead of just file
>         names if
>         that helps.
>         
>         Thanks - and I'm sure that as soon as I see a response or two
>         I'll do
>         the old V8 slap to the forehead and wonder how I could be so
>         dumb.
>         
>         --
>         Ken Weinert
>         http://quarter-flash.com
>         
>         Please avoid sending me Word or PowerPoint attachments
>         http://www.gnu.org/philosophy/no-word-attachments.html
>         
>         _______________________________________________
>         Web Page:  http://lug.boulder.co.us
>         Mailing List:
>         http://lists.lug.boulder.co.us/mailman/listinfo/lug
>         Join us on IRC: lug.boulder.co.us port=6667 channel=#colug
> 
> 
> 
> -- 
> Jeffrey Haemer <jeffrey.haemer at gmail.com>
> 720-837-8908 [cell]
> http://goyishekop.blogspot.com
-- 
Ken Weinert
http://quarter-flash.com

Please avoid sending me Word or PowerPoint attachments
http://www.gnu.org/philosophy/no-word-attachments.html
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
URL: <http://lists.lug.boulder.co.us/pipermail/lug/attachments/20080220/9985d4d2/attachment.pgp>


More information about the LUG mailing list