• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

Simple shell script

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

Arkaine23

Captain Random Senior Evil
Joined
Nov 8, 2001
I need to write a little script that finds the sum and average of the arguments entered on the command line.

scriptname 34 76 139 57 327
The sum is $sum
The average is $avg

I'd also like to write a script that can tell how many Sundays there were in the year 2000.
 
scripting practice

Ok, I figured out the first one.

#!/bin/sh
# find sum of command line arguments
a=0
for x in $*
do
a=`expr $a + $x`
done
echo Total is $a
# find and print average
b=`echo "scale=2; $a / $#" | bc`
echo Average is $b
 
Back