LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-06-2005, 04:41 PM   #1
qerf
LQ Newbie
 
Registered: Mar 2004
Posts: 13

Rep: Reputation: 0
shell programming


i have to calculate mean value using shell programming.
ex:
./mean 11 15 18 19 41 45
but i cant succeed the getting arguments. The problem is count of arguments. I search web and see examples they are doing like that
n1=$1
n2=$2
n3=$3
...
..
but in my situation, number of arguments is unknown i mean, there will be only two or one hundred arguments. how cant i get arguments in this situation?
show me the rigth way thnx...
 
Old 11-06-2005, 05:38 PM   #2
TBC Cosmo
Member
 
Registered: Feb 2004
Location: NY
Distribution: Fedora 10, CentOS 5.4, Debian 5 Sparc64
Posts: 355

Rep: Reputation: 43
Try using shift, described here
 
Old 11-06-2005, 05:48 PM   #3
Andrew Benton
Senior Member
 
Registered: Aug 2003
Location: Birkenhead/Britain
Distribution: Linux From Scratch
Posts: 2,073

Rep: Reputation: 64
Code:
#!/bin/bash
for i in $*
do total=$(($total+$i))
echo $total
done
echo $(($total/$#))
 
Old 11-06-2005, 05:51 PM   #4
Mad Scientist
Member
 
Registered: May 2003
Posts: 167

Rep: Reputation: 30
Here's something that will work.

Code:
#!/bin/bash
# Compute the average of the numbers in file 'input'

SUM=0
NUM=0

for i in `sed -n p input`
do
let "SUM=$SUM+$i"
let "NUM=$NUM+1"
done

let "AVG=$SUM/$NUM"

echo $AVG

exit 0
You can name this file "avg". Then, place all the numbers you want to average in a file called "input", but make sure that there is only one number appearing on each line. For example, you could have a file "input" with the contents

Code:
1
2
3
4
5
And if you named your script "avg", and in the same directory you have the file "input" with the above contents, then from this directory can type

Code:
sh avg
and the number 3 should be printed to the terminal.
 
Old 11-06-2005, 05:54 PM   #5
Mad Scientist
Member
 
Registered: May 2003
Posts: 167

Rep: Reputation: 30
Whoops, Andrew got his in a few minutes before mine, and I didn't see it. His is simpler, and takes input more in the way you were asking for them.
 
Old 11-06-2005, 06:28 PM   #6
Andrew Benton
Senior Member
 
Registered: Aug 2003
Location: Birkenhead/Britain
Distribution: Linux From Scratch
Posts: 2,073

Rep: Reputation: 64
Yes, but it doesn't do decimals, I had to use bc for that
Code:
#!/bin/bash
total=0
for i in $*
do
total=$(echo "scale=3; $total+$i" | bc)
done
echo "average="$(echo "scale=3; $total/$#" | bc)

Last edited by Andrew Benton; 11-06-2005 at 08:09 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell programming quan Programming 8 11-27-2005 08:46 PM
programming shell tuls Slackware 3 08-24-2005 06:58 PM
new to shell programming bluetwist Debian 3 08-01-2005 04:56 PM
Shell Programming eantoranz Linux - Networking 2 06-02-2005 12:21 PM
shell programming shadowsurfer Linux - Newbie 3 09-09-2004 10:40 PM


All times are GMT -5. The time now is 04:30 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration