0

I want a script to calculate the sum of the numbers entered by the user.

so far I have:

#!/bin/bash
add() {
  sum=0

  for num
  do
          sum=$((sum +num))
  done

}

echo "$sum"

the command I use is:

add 4 -3 9 12

the return is: blank line - not the words just a blank line username~/folder:>

what am I missing?

permissions are rwxr xr x I'm running an interactive shell with the folder in my path, I'm at a loss.

  • Why should it output anything else than a blank line? The script defines a function add(), does not do anything with it, then echoes the unset variable $sum. – AlexP Dec 20 '17 at 03:04

2 Answers2

2

Let's modify your script slightly:

$ cat add.sh
#!/bin/bash
add() {
  sum=0
  for num
  do
          sum=$((sum +num))
  done
}

add "$@"
echo "sum=$sum"

Now, let's run the script:

$ bash add.sh 4 -3 9 12
sum=22

It works.

The only change is that we called your function via add "$@" before echoing sum.

John1024
  • 74,655
1

Just to include the actual simple way to do this:

#!/bin/bash
IFS=+
bc <<< "$*"

And using it (when in a file called add.sh on which you have execute permissions):

$ ./add.sh 4 -3 9 12
22
$ 

Or just define it as a function in your ~/.bashrc or in the larger bash script you're writing:

add() (
  IFS=+
  bc <<< "$*"
)
Wildcard
  • 36,499
  • @ilkkachu, sure, well, the other approach is only simple if you know that bash functions don't have their own variable scope (when defined with curly braces), if you know how "$@" and for num do interact, if you understand arithmetic expansion AND how and why $ is not needed therein, AND if every future reader of the script knows all of that, too! :P – Wildcard Dec 20 '17 at 04:46
  • right, my point was mostly on features that are similar to other programming languages vs. somewhat quaint features specific to the shell language. – ilkkachu Dec 20 '17 at 04:59
  • @ilkkachu, to quote Stephane Chazelas: "In C or most other languages, building blocks are just one level above computer instructions. You tell your processor what to do and then what to do next. You take your processor by the hand and micro-manage it: you open that file, you read that many bytes, you do this, you do that with it.

    Shells are a higher level language. One may say it's not even a language. They're before all command line interpreters. The job is done by those commands you run and the shell is only meant to orchestrate them."

    – Wildcard Dec 20 '17 at 05:05
  • Point being: nearly all errors people make in writing shell scripts are based on the fundamental misconception that the shell is a typical programming language like many others, when it's really not—at all. – Wildcard Dec 20 '17 at 05:07
  • well, simplicity depends on the context and on how familiar the thing is to someone. Judging by most questions here, not too many know the difference between $@ and $*, let alone to use $* as a synonym for "join". So in the context of a Q where someone tried the straightforward implementation, but still forgot a function call, I'm not really sure that's "simple". Especially since you didn't give any explanation or reference. Stuff like $* or <<< is next-to-impossible to search for... – ilkkachu Dec 20 '17 at 09:13