I want to add a list of numbers in a file. Here's what I've tried.
Please help. Thanks:
#!/bin/ksh
#Script name: myscript
sum=0
while read -u3 number_of_records
do
print "$number_of_records"
((sum+=number_of_records))
done
print "$sum"
3<$1
------------
$cat myfile
20
30
10
----------
$myscript myfile
|
|
0
|
|
|
|
Reply
|
rafleyb
|
6/8/2004 6:28:54 PM |
|
In article <656a0530.0406081028.29203402@posting.google.com>,
rafleyb@hotmail.com (Rafley) wrote:
> I want to add a list of numbers in a file. Here's what I've tried.
> Please help. Thanks:
>
> #!/bin/ksh
> #Script name: myscript
> sum=0
> while read -u3 number_of_records
> do
> print "$number_of_records"
> ((sum+=number_of_records))
> done
> print "$sum"
> 3<$1
>
> ------------
> $cat myfile
> 20
> 30
> 10
> ----------
> $myscript myfile
You're redirecting the input to a null command that's executed after the
loop is finished and the sum printed. The redirection should be on the
"done" line if you want it to be in effect during the while-loop.
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
|
|
0
|
|
|
|
Reply
|
Barry
|
6/8/2004 8:55:00 PM
|
|