0

Here is my current code:

for i do
    sum=$(expr $sum + $i)
done
echo =$sum

Now this works, but I want it to display all of the numbers. For example, If I enter ./sum 1 2 3, I want it to display 1+2+3=6. Right now it only displays the answer. Also, is there a way I could execute the file without ./. For instance, could I use sum 1 2 3 instead of ./sum 1 2 3. I've tried chmod 700 "myfile," but that didn't seem to work.

4 Answers 4

1

What about this?

while (( $# > 1 )); do
    printf "$1 + "
    sum=$((sum + $1 ))
    shift
done
echo "$1 = $((sum + $1))"

It loops through the arguments you provide and adds them into the variable $sum. For stylistic purposes I use printf and finally echo to have everything in the same line.

The usage of shift is explained here:

The shift builtin command is used to "shift" the positional parameters by the given number n or by 1, if no number is given.

Test

$ sh script.sh 1 2 3
1 + 2 + 3 = 6
$ sh script.sh 1 2 3 4 5
1 + 2 + 3 + 4 + 5 = 15
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! It works perfectly, but what exactly did you do to get it to run without ./?
I use sh or bash to call it. It is like when you say python script.py, etc. You can also check chepner's answer to see how to run it without not even sh.
1

You can use IFS and $* to join all the arguments with +:

(IFS="+"; printf '%s' "$*")

sum=0
for i; do
    sum=$((sum + i))
done
printf '=%d\n' "$sum"

To run without the ./ prefix, you need to put the program in a directory that appears in your PATH. Unless this is intended for people other than you, you should create a bin directory in your home directory, then add this to your .bash_profile:

PATH=~/bin/:$PATH

so that your bin directory is added to the list of places to look for commands.

4 Comments

or IFS="+"; echo $(($(printf '%s' "$*")))?
This one also works. How would I put it in a directory that appears in my PATH? Sorry I'm a bit new to this.
@GodMode: or put this function in your .bashrc: sum() { IFS="+"; echo $(($(printf '%s' "$*"))); } and use it this way: sum 1 2 3
It's a bad idea to set IFS globally; that's why I made the original change in a subshell.
0

Another answer:

sum=0
for i in $@
do
    sum=$(($sum + $i))
done
echo $sum

4 Comments

Yup!, this one also works, but I noticed I have to use the ./ prefix for this one whereas the one containing the while loop, I didn't.
If you use sh or bash to call it then you won't need to prefix ./
Yes, I ran it in bash, and sh, but I get the error "No such file or directory." for each number I input on the command line.
Save the code to a file and do something like sh /path/to/script.sh 1 2 3 4 5. You can also add the method to your .bashrc or place the script in your PATH.
0

Here a solution without loops:

numbers=$( printf -v str '%s+' $@; printf '%s' "${str%+}" )
sum=$(( $numbers ))
echo $numbers=$sum 

the first line joins the input arguments with +, the second line calculates the sum and the third line outputs the equation

as to the second part of your question. Just put the script somewhere in your path (for example /usr/local/bin)

3 Comments

Ok, I understand, but the script is in my Home directory. Is that not in my path?
@GodMode No, the default path doesn't normally include any per-user directories.
you can find out which directories are in your path by typing echo $PATH. This will give you a list of paths separated by colons (for example /bin:/usr/bin:/usr/local/bin. The default path for programs that you install yourself is /usr/local/bin.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.