Average of arguments


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Average of arguments
# 1  
Old 08-08-2002
Average of arguments

I have a program where the user enters numbers as arguments and it calculates the average, but it only works when i have a set amount of arguments that can be entered. How can i alter my script so that the user can enter as many arguments as he wishes?
# 2  
Old 08-08-2002
You don't tell us which shell you are using. But in ksh, bash, and sh, $# is the number of arguments entered. To process an arbritary number of arguments, you typically loop checking $#. Inside the loop, you process $1. Then use use the command "shift". "shift" discards $1 and kinda moves the other args over. And it decrements $#.
# 3  
Old 08-09-2002
Cheers, I got it workin fine now!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Help with average calculation.

i have a file with 2 columns. i want to calculate the average of column 1 based on the values of column 2. here's how the file looks like. i want to calculate the sums of numbers corresponding to 1 and then calculate the average. same for numbers corresponding to zero. any help with a code would... (1 Reply)
Discussion started by: onerokeyz
1 Replies

2. UNIX for Dummies Questions & Answers

Calculating average

Hi I have file like below 111,victor,48,12,36 342,Peter,54,58,30 476,Scott,25,36,48 567,Patty,74,17,95 I have written below code to calcualte avereage for every id Victor = 48+12+36/3 #!/bin/ksh /usr/xpg4/bin/awk ' BEGIN {FS=","} {sum=0; n=0;i=3 (1 Reply)
Discussion started by: stew
1 Replies

3. Shell Programming and Scripting

Loop for average

I am trying to write a loop script in bash thru umbuntu for the following: ask for a password if wrong show a message if wright then asks for 3 numbers from you then calculates the average of them at the end it saves the average and the date of the calculation in a text file and then zips it.... (1 Reply)
Discussion started by: redmond1212
1 Replies

4. Shell Programming and Scripting

Average for every day

Hi i have data like - Fieldseperator is "\t" -.. ... 05/18/12-23:40 12.0647 96.4762 140.746 19.4222 38.0837 17.1549 05/18/12-23:50 11.9463 97.7457 139.447 20.4776 29.8511 17.0144 05/19/12-00:00 11.6922 94.7384 130.364 18.5693 28.28 15.6425 05/19/12-00:10 10.8512 87.547 113.844... (11 Replies)
Discussion started by: IMPe
11 Replies

5. Shell Programming and Scripting

grep with two arguments to arguments to surch for

Hello, is it possible to give grep two documents to surche for? like grep "test" /home/one.txt AND /home/two.txt ? thanks (1 Reply)
Discussion started by: Cybertron
1 Replies

6. UNIX for Dummies Questions & Answers

Calculating average

Hi, i have 12 float variables in a bash file and i want to calculate the average of them. Can any body help? (6 Replies)
Discussion started by: limadario
6 Replies

7. Shell Programming and Scripting

Average in awk

Hi I am looking for an awk script which can compute average of all the fields every 5th line. The file looks: A B C D E F G H I J K L M 1 18 13 14 12 14 13 11 12 12 15 15 15 2 17 17 13 13 13 12 12 11 12 14 15 14 3 16 16 12 12 12 11 11 12 11 16 14 13 4 15 15 11 11 11 12 11 12 11... (6 Replies)
Discussion started by: saint2006
6 Replies

8. Shell Programming and Scripting

count average

Hi Friends, Can any one help me with count average of student marks in this file (i can not change structure of the input file): input file: 1:John Smith:2 3 4 5 2:Mark Anderson:3 2 3:Susan Waterman:2 4 2 (numbers of marks are different) output: Name:John Smith ID#: 1 Avg. mark:... (6 Replies)
Discussion started by: mleplawy
6 Replies

9. UNIX for Dummies Questions & Answers

average value

If I have a file like this, could anyone please guide me how to find the average value in each metrix. The file has got about 130,000 metrixs. Grid-ref= 142, 235 178 182 203 240 273 295 289 293 283 262 201 176 167 187 187 246 260 282 299 312 293 276 230 191 169 ... (2 Replies)
Discussion started by: su_in99
2 Replies

10. Shell Programming and Scripting

Calculating the average

This is the cronjob ---------------------- root@a7germ:/home/paxtemp > crontab -l|grep test 57 * * * * /home/paxtemp/test_1.sh 0,5,10,15,20,25,30,35,40,45,50,55 * * * * /home/paxtemp/test.sh root@a7germ:/home/paxtemp > This is the contents of test.sh script... (2 Replies)
Discussion started by: kekanap
2 Replies
Login or Register to Ask a Question