[Gllug] Small shell script challenge

Tethys tet at createservices.com
Tue Dec 16 17:09:03 UTC 2003


Richard Jones writes:

>Using something like:
>
>ls -lR dir | awk '{print $5}' | ....
>
>I can get a list of numbers (actually including some blank
>lines). What's the quickest way to add up those numbers?

As Alain has said, just use awk to total stuff up for you. But if you
want to do it from a shell script, there are several ways. For example:

    ls -lR dir | awk '{print $5}' | \
    while read a; do [ ! -z "$a" ] && tot=$(($tot + $a)); echo "$tot"; done | \
    tail -1

Alternatively:

    ls -lR dir | awk '{print $5}' | sed 's/$/ +p/' | dc 2>/dev/null | tail -1

Or slightly differently:

	(ls -lR tmp/ | awk '{print $5}' | sed 's/$/ +/'; echo p) | dc 2>/dev/null

Tet
-- 
Gllug mailing list  -  Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug




More information about the GLLUG mailing list