Shell Script not working using for loop and a funtion


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell Script not working using for loop and a funtion
# 1  
Old 02-09-2010
Shell Script not working using for loop and a funtion

Hi-

Here is the shell script that for some reason is not returning results:
Code:
#! /bin/ksh -

avg()   {
        AVG=0
        typeset SUM=0
        if [ "$#" -lt 2 ]
        then
                echo "You must have at least two numbers"
        else
        for NUM in "$*"
        do
                SUM=$(( SUM + $NUM ))
                echo "SUM: $SUM + $NUM"
        done
                AVG=`echo "scale=2; $SUM/$#"|bc`
        fi
}

ANS=$(avg $*)
echo "Average of $@ is $ANS"

If you run it would simply say "Average of numbers (list the numbers) is"
and give me this error
Code:
"./avg.sh: line 13:  SUM + 5 2 : arithmetic syntax error"

Thanks

Last edited by Scott; 02-09-2010 at 09:35 AM.. Reason: Please use code tags
# 2  
Old 02-09-2010
don't quote the $*
and you should return the $AVG instead the "SUM: $SUM + $NUM"

try:

Code:
avg()   {
        AVG=0
        typeset SUM=0
        if [ "$#" -lt 2 ]
        then
                echo "You must have at least two numbers"
        else
        for NUM in $*
        do
                SUM=$(( SUM + $NUM ))
                #echo "SUM: $SUM + $NUM"
        done
                AVG=`echo "scale=2; $SUM/$#"|bc`
                echo $AVG
        fi
}

ANS=$(avg $*)
echo "Average of $@ is $ANS"

# 3  
Old 02-09-2010
Yes, it worked. Thanks for your help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

For loop in bourne shell is not working

I have a loop with cases I am working on Bourne shell for file in *.${Today}*.csv *.${Today}*.txt\ do case ${file} in sun_detail) do something ;; sum) do something ;; mod) do something ;; *) do something ;; (5 Replies)
Discussion started by: digioleg54
5 Replies

2. Shell Programming and Scripting

Cp not working in shell script but running perfectly from shell

Dear All, I have script. Dest="" IFS=' ' for translation in $(echo $MY_MAP) do t1=$(echo $translation | cut -d"=" -f1) t2=$(echo $translation | cut -d"=" -f2| cut -d"," -f1) if then Dest=$UNX/$u_product_path/$u_study_path/$UNXTR/$t2 break; ... (4 Replies)
Discussion started by: yadavricky
4 Replies

3. Shell Programming and Scripting

Shell script loop not working

Hi I'm using this script to transcode videos in an Ubuntu 12.04 machine. #! /bin/bash MOVDIR="/media/topaz_1/media/transcodes/transcode_mov/" MOVDESTDIR="/media/topaz_1/media/transcodes/final_mov/" DONEFILESDIR="/media/topaz_1/media/transcodes/dv_cache/"... (1 Reply)
Discussion started by: oscarfelson
1 Replies

4. Shell Programming and Scripting

[Solved] Simple script not working- for loop

Hi all, Please guide me writing this script Follwing is the file which I have created, which contains the files to be copied. cat system1-dr.txt /etc/passwd /etc/shadow /etc/group /etc/vfstab /etc/profile /etc/default/init /etc/shells /etc/dfs/dfstab /etc/dfs/sharetab... (11 Replies)
Discussion started by: manalisharmabe
11 Replies

5. Shell Programming and Scripting

expect script inside shell script not working.

Shell Scipt: temp.sh su - <$username> expect pass.exp Expect script: pass.exp #!/usr/bin/expect -f # Login ####################### expect "Password: " send "<$password>\r" it comes up with Password: but doesnt take password passed throguh file. (2 Replies)
Discussion started by: bhavesh.sapra
2 Replies

6. UNIX for Dummies Questions & Answers

shell Script working

i wrote a shell program in Home Directory. and i changed to other directory. i want to try to execute shell script in Other Dir. it is not executed. how can i make this script to execute in other directory also?? Thanks, Arun (11 Replies)
Discussion started by: arun508.gatike
11 Replies

7. Shell Programming and Scripting

foreach loop working in terminal but not in the script

Hi, I am new here I have used the forums a long time to search for things they are very helpful. I have unfortunately used up all my resources (professors, grad students) and need some help. I have this very simple piece of code (using to isolate the problem) in a csh script: #!/bin/csh... (12 Replies)
Discussion started by: bflinchum
12 Replies

8. Shell Programming and Scripting

Perl script 'system' linking to local shell script not working

Trying to figure out why this works: printpwd.pl #!/usr/bin/perl use CGI::Carp qw( fatalsToBrowser ); print "Content-type: text/html\n\n"; $A = system("pwd"); $A = `pwd`; print "$A\n"; ^^actually that works/breaks if that makes any sense.. i get the working directory twice but when... (5 Replies)
Discussion started by: phpfreak
5 Replies

9. UNIX for Dummies Questions & Answers

Anyone know?: How the 'for'-loop could stop working in interactive bash shell?!

It is happening with my sessions already second time: a 'for'-loop for some reason stop to work as expected. That means or it is looping without exitting, or it is not loop even once. Here example of my try when it is not processing even one loop. You can see, I start new subshell and... (14 Replies)
Discussion started by: alex_5161
14 Replies

10. Shell Programming and Scripting

if loop not working in BASH shell

i have this code for a simple if loop: #!/bin/bash array="1 2 3 4 5" array2="5 6 7 8 9" if } -gt ${array} ]; then echo "${array2} is greater than ${array}!!" fi the error is ./script8: line 9: [: too many arguments ./script8: line 9: [: too many arguments ./script8: line 9: [:... (10 Replies)
Discussion started by: npatwardhan
10 Replies
Login or Register to Ask a Question