Shell script to find the sum of argument passed to the script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script to find the sum of argument passed to the script
# 1  
Old 07-15-2012
Shell script to find the sum of argument passed to the script

I want to make a script which takes the number of argument, add those argument and gives output to the user, but I am not getting through...

Script that i am using is below :
Code:
#!/bin/bash
sum=0
for i in $@
do
    sum=$sum+$1
    echo $sum
    shift
done

I am executing the script as below
Code:
sh script 4 5 6 7

the output that I am getting is
Code:
0+4
0+4+5
0+4+5+6
0+4+5+6+7

# 2  
Old 07-15-2012
Code:
#!/bin/bash
sum=0
for i in $@; do sum=$((sum+i)); done
echo $sum
exit 0

Code:
~/unix.com$ bash script 4 5 6 7
22

See man bash for more infos
Quote:
((expression))
The expression is evaluated according to the rules described
below under ARITHMETIC EVALUATION. If the value of the expres-
sion is non-zero, the return status is 0; otherwise the return
status is 1. This is exactly equivalent to let "expression".
This User Gave Thanks to tukuyomi For This Post:
# 3  
Old 07-15-2012
may be this ?

Code:
$ cat script.sh
#!/bin/bash
sum=0
for i in $@
do
    sum=`expr $sum + $i`
    echo $sum
    shift
done

output:

Code:
$ ./script.sh 4 5 6 7

4
9
15
22

# 4  
Old 07-15-2012
Code:
printf '%s\n' "$@" | paste -sd+ - | bc

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Getting number of argument passed to a shell script

Hi Experts, I have been trying to work on a simple shell script that will just add the two argument passed to it. Here is what i tried : #!/bin/bash welcome(){ echo "Welcome to this Progg. which will accept two parameter" } main_logic(){ arg=$# echo "Number of argument passed is... (4 Replies)
Discussion started by: mukulverma2408
4 Replies

2. Shell Programming and Scripting

Shell script to create runtime variables based on the number of parameters passed in the script

Hi All, I have a script which intends to create as many variables at runtime, as the number of parameters passed to it. The script needs to save these parameter values in the variables created and print them abc.sh ---------- export Numbr_Parms=$# export a=1 while do export... (3 Replies)
Discussion started by: dev.devil.1983
3 Replies

3. Shell Programming and Scripting

How to pass Oracle sql script as argument to UNIX shell script?

Hi all, $ echo $SHELL /bin/bash Requirement - How to pass oracle sql script as argument to unix shell script? $ ./output.sh users.sql Below are the shell scripts and the oracle sql file in the same folder. Shell Script $ cat output.sh #!/bin/bash .... (7 Replies)
Discussion started by: a1_win
7 Replies

4. Shell Programming and Scripting

Shell script that check the argument passed to it and prints error if test condition is not met

I want to make a script that check for the argument passed to it and generates an error in case any character/string argument passed to it. I am using below code, but its not working. can anyone help. #!/bin/bash if ]; then echo 'An integer argument is passed to the script hence... (3 Replies)
Discussion started by: mukulverma2408
3 Replies

5. Shell Programming and Scripting

How we can pass the argument when calling shell script from perl script

Can someone let me know how could I achieve this In one of per script I am calling the shell script but I need to so one thing that is one shell script call I need to pass pne argument.In below code I am calling my ftp script but here I want to pass one argument so how could I do this (e.g:... (5 Replies)
Discussion started by: anuragpgtgerman
5 Replies

6. Shell Programming and Scripting

shell script for ftp files passed in command line argument

i want to write a shell script function that will ftp the files passed in the command line . i have written a shell script for ftp but how will it do for all files passed in command line argument , i am passing 4 files as argument ./ftp.sh file1 file2 file3 file4 code written by me... (5 Replies)
Discussion started by: rateeshkumar
5 Replies

7. Shell Programming and Scripting

Getting the file name passed to a shell script

Hi all I want to use the filename passed into a shell script within the shell it self. The file will be passed as shown below ./myScript < myFile.file i tried $1 but i think since myFile is not passed as a command line arg its not saving the file name in $1 any help is appreciated. (5 Replies)
Discussion started by: sridanu
5 Replies

8. Homework & Coursework Questions

Help with shell script to find sum of first n numbers of Fibonacci series

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Shell script to find sum of first n numbers of Fibonacci series 2. Relevant commands, code, scripts,... (0 Replies)
Discussion started by: Kshitija
0 Replies

9. Shell Programming and Scripting

Shell script to find the sum of first n Fibonacci numbers

pls give me the solution for this i need it for my exam pls pls pls Shell script to find the sum of first n Fibonacci numbers (1 Reply)
Discussion started by: Kshitija
1 Replies

10. Shell Programming and Scripting

how to find the path of a file when it is passed as ....filename(parameter) to script

hi unix guru's..................:confused: question is posted in the #3 permalink shown below. (3 Replies)
Discussion started by: yahoo!
3 Replies
Login or Register to Ask a Question