ftp.nice.ch/peanuts/GeneralData/Usenet/news/1991/CSNSys-91.tar.gz#/comp-sys-next-sysadmin/1991/Oct/Adding-numbers-with-a-shell-script.

This is Adding-numbers-with-a-shell-script. in view mode; [Up]


Date: Sun 02-Oct-1991 17:42:30 From: rich@vaxkiller.agi.org (Richard E. Showalter) Subject: Adding numbers with a shell script. Hello all, Forgive me if this is a simple minded question but I would like to be able to add a list of numbers from a file to get a total to output to another file from a shell script. What I am doing is running du from a cron script to output the amount of diskspace being used by each user each day. At the end of the month I would like to be able to add up those daily figures to get a Mb/day diskstorage/user figure for accounting. I am a novice UNIX SysAdmin so any help would be most appreciated. I have RTFM about bc and dc but I can not figure out for the live of me how to feed these programs the info I get and output a total figure. Thanks in advance.
Date: Sun 03-Oct-1991 11:56:12 From: lrb@rri.uwo.ca (Lance R. Bailey) Subject: Re: Adding numbers with a shell script. In article <685@nic.cerf.net> rich@vaxkiller.agi.org (Richard E. Showalter) >... I would like to be >able to add a list of numbers from a file to get a total to output to another >file from a shell script. here, adds any ammount of numbers on a line, or on successive lines. #!/bin/sh # add some numbers (echo 0 ;( adjust -m1 | awk ' { print $1 "+" } ' ) ; echo p) | dc
Date: Sun 03-Oct-1991 14:46:36 From: rich@vaxkiller.agi.org (Richard E. Showalter) Subject: Re: Adding numbers with a shell script. In article <685@nic.cerf.net> rich@vaxkiller.agi.org (Richard E. Showalter) > Hello all, > > Forgive me if this is a simple minded question but I would like to be > able to add a list of numbers from a file to get a total to output to another > file from a shell script. What I am doing is running du from a cron script to > output the amount of diskspace being used by each user each day. At the end of > the month I would like to be able to add up those daily figures to get a Mb/day > diskstorage/user figure for accounting. I am a novice UNIX SysAdmin so any > help would be most appreciated. I have RTFM about bc and dc but I can not > figure out for the live of me how to feed these programs the info I get and > output a total figure. Thanks in advance. > Thank you to all who responded. I now have 50 ways to add numbers using "awk" or "gawk". awk '{s += $1} END {print s}'
Date: Sun 04-Oct-1991 17:24:21 From: robertl@rlaferla.lotus.com (Robert La Ferla) Subject: Re: Adding numbers with a shell script. Rich, One of the best ways to do system accounting such as diskspace totals is by using the AWK programming language. You can look at the man page for awk and I strongly suggest the purchase of "The AWK Programming Language" by Aho, Weinberger, and Kernighan (UNIX trivia: thus the name "A.W.K") It is published by Addison-Wesley. Here's an example of using awk and du together: % ls -agl sendmail total 61 drwxr-xr-x 2 root atg 1024 Sep 9 16:41 ./ drwxr-xr-x 31 robertl atg 2048 Oct 3 14:11 ../ -rw-rw-r-- 1 root atg 1168 Aug 8 1990 aliases -rw-r--r-- 1 root atg 0 Dec 17 1990 aliases.dir -rw-r--r-- 1 root atg 1024 Dec 17 1990 aliases.pag -rw-rw-r-- 1 root atg 9061 May 21 14:01 sendmail.cf -r--r--r-- 1 root atg 9786 Nov 11 1990 sendmail.mailhost.cf -r--r--r-- 1 root atg 8548 Nov 11 1990 sendmail.sharedsubsidiary.cf -r--r--r-- 1 root atg 8295 Nov 11 1990 sendmail.subsidiary.cf -rw-r--r-- 1 root atg 8917 Nov 21 1990 sendmail_atg.cf -rw-rw-r-- 1 root atg 9061 May 21 14:01 sendmail_test.cf % du -a sendmail 10 sendmail/sendmail.mailhost.cf 9 sendmail/sendmail.subsidiary.cf 9 sendmail/sendmail.sharedsubsidiary.cf 9 sendmail/sendmail.cf 2 sendmail/aliases 0 sendmail/aliases.dir 1 sendmail/aliases.pag 9 sendmail/sendmail_atg.cf 9 sendmail/sendmail_test.cf 59 sendmail % du -a sendmail/* | awk 'BEGIN {total = 0} {total = total + $1} END {print total}' The awk script above is: BEGIN { total = 0 } {total = total + $1} END { print total } All initialization is down in the BEGIN code segment (i.e. in between the { } ) The total is calculated by adding up the first column (field) piped in from the du command. This field is denoted by $1. Finally, the total is printed in the END code segment. As to a direct answer to your question, another mechanism you can use is the "@" command in csh. It's not as flexible as awk but it may come in handy. Briefly, you know how you can set variables in the csh using "set"? Well, for numeric variables, you have the "@" @ variable = expression For example: % @ sum = 1 + 2 % echo $sum 3 Also, here's an csh alias that I find useful. It generates a TOP 20 list of the largest files/subdirectories that users have. The "/usr/homes" is a network directory where everyone's home directories are located. alias diskhogs 'du /usr/homes | sort -n -r | head -20' In article <685@nic.cerf.net> rich@vaxkiller.agi.org (Richard E. Showalter) writes: > Hello all, > > Forgive me if this is a simple minded question but I would like to be > able to add a list of numbers from a file to get a total to output to another > file from a shell script. What I am doing is running du from a cron script to > output the amount of diskspace being used by each user each day. At the end of > the month I would like to be able to add up those daily figures to get a Mb/day > diskstorage/user figure for accounting. I am a novice UNIX SysAdmin so any > help would be most appreciated. I have RTFM about bc and dc but I can not > figure out for the live of me how to feed these programs the info I get and > output a total figure. Thanks in advance. Robert La Ferla Lotus Development Corporation Advanced Technology Group / Improv robertl@lotus.com

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Marcel Waldvogel and Netfuture.ch.