Issues with script for adding numbers
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I am having issues submitting my script right now. I wonder if any of you guys have any ideas on why it is not taking it as it is working currently? It wants to add 6, 9, -4 and 7 together and my script is doing that.. Maybe there are ways to improve it? Any ideas would help out a lot.
#!/bin/bash
sum=0
for number in $1 $2 $3 $4
do
sum=$(($1 + $2 + $3 + $4))
echo $number
done
echo $sum
shell-script scripting
add a comment |Â
up vote
1
down vote
favorite
I am having issues submitting my script right now. I wonder if any of you guys have any ideas on why it is not taking it as it is working currently? It wants to add 6, 9, -4 and 7 together and my script is doing that.. Maybe there are ways to improve it? Any ideas would help out a lot.
#!/bin/bash
sum=0
for number in $1 $2 $3 $4
do
sum=$(($1 + $2 + $3 + $4))
echo $number
done
echo $sum
shell-script scripting
1
Why are you looping across the positional parameters and summing all of them up four times? What happens with your script as written if you only give it three values to add?
â DopeGhoti
Mar 23 at 19:13
1
Why the loop?sum=$(($1 + $2 + $3 + $4))
is enough IMHO.
â nohillside
Mar 23 at 19:14
I suggest you please take a look at this Q/A on AU as you highly need to have a sum function here.
â Ã±ÃÂsýù÷
Mar 23 at 19:22
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am having issues submitting my script right now. I wonder if any of you guys have any ideas on why it is not taking it as it is working currently? It wants to add 6, 9, -4 and 7 together and my script is doing that.. Maybe there are ways to improve it? Any ideas would help out a lot.
#!/bin/bash
sum=0
for number in $1 $2 $3 $4
do
sum=$(($1 + $2 + $3 + $4))
echo $number
done
echo $sum
shell-script scripting
I am having issues submitting my script right now. I wonder if any of you guys have any ideas on why it is not taking it as it is working currently? It wants to add 6, 9, -4 and 7 together and my script is doing that.. Maybe there are ways to improve it? Any ideas would help out a lot.
#!/bin/bash
sum=0
for number in $1 $2 $3 $4
do
sum=$(($1 + $2 + $3 + $4))
echo $number
done
echo $sum
shell-script scripting
edited Mar 23 at 20:18
Kusalananda
102k13201317
102k13201317
asked Mar 23 at 19:09
pvman
61
61
1
Why are you looping across the positional parameters and summing all of them up four times? What happens with your script as written if you only give it three values to add?
â DopeGhoti
Mar 23 at 19:13
1
Why the loop?sum=$(($1 + $2 + $3 + $4))
is enough IMHO.
â nohillside
Mar 23 at 19:14
I suggest you please take a look at this Q/A on AU as you highly need to have a sum function here.
â Ã±ÃÂsýù÷
Mar 23 at 19:22
add a comment |Â
1
Why are you looping across the positional parameters and summing all of them up four times? What happens with your script as written if you only give it three values to add?
â DopeGhoti
Mar 23 at 19:13
1
Why the loop?sum=$(($1 + $2 + $3 + $4))
is enough IMHO.
â nohillside
Mar 23 at 19:14
I suggest you please take a look at this Q/A on AU as you highly need to have a sum function here.
â Ã±ÃÂsýù÷
Mar 23 at 19:22
1
1
Why are you looping across the positional parameters and summing all of them up four times? What happens with your script as written if you only give it three values to add?
â DopeGhoti
Mar 23 at 19:13
Why are you looping across the positional parameters and summing all of them up four times? What happens with your script as written if you only give it three values to add?
â DopeGhoti
Mar 23 at 19:13
1
1
Why the loop?
sum=$(($1 + $2 + $3 + $4))
is enough IMHO.â nohillside
Mar 23 at 19:14
Why the loop?
sum=$(($1 + $2 + $3 + $4))
is enough IMHO.â nohillside
Mar 23 at 19:14
I suggest you please take a look at this Q/A on AU as you highly need to have a sum function here.
â Ã±ÃÂsýù÷
Mar 23 at 19:22
I suggest you please take a look at this Q/A on AU as you highly need to have a sum function here.
â Ã±ÃÂsýù÷
Mar 23 at 19:22
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
2
down vote
If you only want to add 4 numbers, this should be enough:
sum()
printf '%dn' "$(( $1 + $2 + $3 + $4 ))"
Or if you want to add an arbitrary amount of numbers:
sum()
local number sum
for number; do
(( sum += number ))
done
printf '%dn' "$sum"
add a comment |Â
up vote
1
down vote
Make the shell work for you. And use external utilities when called for.
#!/bin/bash
IFS=+
echo "$*" | bc
add a comment |Â
up vote
0
down vote
There is no need to loop if you know you're always going to get four integers on the command line:
#!/bin/sh
sum=$(( $1 + $2 + $3 + $4 ))
printf 'sum is %dn' "$sum"
Alternatively, just
#!/bin/sh
printf 'sum is %dn' "$(( $1 + $2 + $3 + $4 ))"
To support an arbitrary number of argument, you will need to loop:
#!/bin/sh
while [ "$#" -gt 0 ]; do
sum=$(( sum + $1 ))
shift
done
printf 'sum is %dn' "$sum"
This script will iterate over its command line arguments, adding the first one to the sum
variable and shifting it off the list of command line arguments, until no arguments are left. The $#
variable expansion will expand to the number of command line arguments, while shift
will remove $1
from the list, shifting $2
into its place (and $3
into $2
etc.).
Alternatively:
#!/bin/sh
for num do
sum=$(( sum + num ))
done
printf 'sum is %dn' "$sum"
Instead of constantly shifting the list of command line arguments, this leaves the list untouch and instead iterates over it, adding each one to sum
in turn.
The for num do
loop head may also be written for num in "$@"; do
.
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
If you only want to add 4 numbers, this should be enough:
sum()
printf '%dn' "$(( $1 + $2 + $3 + $4 ))"
Or if you want to add an arbitrary amount of numbers:
sum()
local number sum
for number; do
(( sum += number ))
done
printf '%dn' "$sum"
add a comment |Â
up vote
2
down vote
If you only want to add 4 numbers, this should be enough:
sum()
printf '%dn' "$(( $1 + $2 + $3 + $4 ))"
Or if you want to add an arbitrary amount of numbers:
sum()
local number sum
for number; do
(( sum += number ))
done
printf '%dn' "$sum"
add a comment |Â
up vote
2
down vote
up vote
2
down vote
If you only want to add 4 numbers, this should be enough:
sum()
printf '%dn' "$(( $1 + $2 + $3 + $4 ))"
Or if you want to add an arbitrary amount of numbers:
sum()
local number sum
for number; do
(( sum += number ))
done
printf '%dn' "$sum"
If you only want to add 4 numbers, this should be enough:
sum()
printf '%dn' "$(( $1 + $2 + $3 + $4 ))"
Or if you want to add an arbitrary amount of numbers:
sum()
local number sum
for number; do
(( sum += number ))
done
printf '%dn' "$sum"
answered Mar 23 at 19:25
nxnev
2,4522423
2,4522423
add a comment |Â
add a comment |Â
up vote
1
down vote
Make the shell work for you. And use external utilities when called for.
#!/bin/bash
IFS=+
echo "$*" | bc
add a comment |Â
up vote
1
down vote
Make the shell work for you. And use external utilities when called for.
#!/bin/bash
IFS=+
echo "$*" | bc
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Make the shell work for you. And use external utilities when called for.
#!/bin/bash
IFS=+
echo "$*" | bc
Make the shell work for you. And use external utilities when called for.
#!/bin/bash
IFS=+
echo "$*" | bc
answered Mar 23 at 19:35
Wildcard
21.9k855153
21.9k855153
add a comment |Â
add a comment |Â
up vote
0
down vote
There is no need to loop if you know you're always going to get four integers on the command line:
#!/bin/sh
sum=$(( $1 + $2 + $3 + $4 ))
printf 'sum is %dn' "$sum"
Alternatively, just
#!/bin/sh
printf 'sum is %dn' "$(( $1 + $2 + $3 + $4 ))"
To support an arbitrary number of argument, you will need to loop:
#!/bin/sh
while [ "$#" -gt 0 ]; do
sum=$(( sum + $1 ))
shift
done
printf 'sum is %dn' "$sum"
This script will iterate over its command line arguments, adding the first one to the sum
variable and shifting it off the list of command line arguments, until no arguments are left. The $#
variable expansion will expand to the number of command line arguments, while shift
will remove $1
from the list, shifting $2
into its place (and $3
into $2
etc.).
Alternatively:
#!/bin/sh
for num do
sum=$(( sum + num ))
done
printf 'sum is %dn' "$sum"
Instead of constantly shifting the list of command line arguments, this leaves the list untouch and instead iterates over it, adding each one to sum
in turn.
The for num do
loop head may also be written for num in "$@"; do
.
add a comment |Â
up vote
0
down vote
There is no need to loop if you know you're always going to get four integers on the command line:
#!/bin/sh
sum=$(( $1 + $2 + $3 + $4 ))
printf 'sum is %dn' "$sum"
Alternatively, just
#!/bin/sh
printf 'sum is %dn' "$(( $1 + $2 + $3 + $4 ))"
To support an arbitrary number of argument, you will need to loop:
#!/bin/sh
while [ "$#" -gt 0 ]; do
sum=$(( sum + $1 ))
shift
done
printf 'sum is %dn' "$sum"
This script will iterate over its command line arguments, adding the first one to the sum
variable and shifting it off the list of command line arguments, until no arguments are left. The $#
variable expansion will expand to the number of command line arguments, while shift
will remove $1
from the list, shifting $2
into its place (and $3
into $2
etc.).
Alternatively:
#!/bin/sh
for num do
sum=$(( sum + num ))
done
printf 'sum is %dn' "$sum"
Instead of constantly shifting the list of command line arguments, this leaves the list untouch and instead iterates over it, adding each one to sum
in turn.
The for num do
loop head may also be written for num in "$@"; do
.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
There is no need to loop if you know you're always going to get four integers on the command line:
#!/bin/sh
sum=$(( $1 + $2 + $3 + $4 ))
printf 'sum is %dn' "$sum"
Alternatively, just
#!/bin/sh
printf 'sum is %dn' "$(( $1 + $2 + $3 + $4 ))"
To support an arbitrary number of argument, you will need to loop:
#!/bin/sh
while [ "$#" -gt 0 ]; do
sum=$(( sum + $1 ))
shift
done
printf 'sum is %dn' "$sum"
This script will iterate over its command line arguments, adding the first one to the sum
variable and shifting it off the list of command line arguments, until no arguments are left. The $#
variable expansion will expand to the number of command line arguments, while shift
will remove $1
from the list, shifting $2
into its place (and $3
into $2
etc.).
Alternatively:
#!/bin/sh
for num do
sum=$(( sum + num ))
done
printf 'sum is %dn' "$sum"
Instead of constantly shifting the list of command line arguments, this leaves the list untouch and instead iterates over it, adding each one to sum
in turn.
The for num do
loop head may also be written for num in "$@"; do
.
There is no need to loop if you know you're always going to get four integers on the command line:
#!/bin/sh
sum=$(( $1 + $2 + $3 + $4 ))
printf 'sum is %dn' "$sum"
Alternatively, just
#!/bin/sh
printf 'sum is %dn' "$(( $1 + $2 + $3 + $4 ))"
To support an arbitrary number of argument, you will need to loop:
#!/bin/sh
while [ "$#" -gt 0 ]; do
sum=$(( sum + $1 ))
shift
done
printf 'sum is %dn' "$sum"
This script will iterate over its command line arguments, adding the first one to the sum
variable and shifting it off the list of command line arguments, until no arguments are left. The $#
variable expansion will expand to the number of command line arguments, while shift
will remove $1
from the list, shifting $2
into its place (and $3
into $2
etc.).
Alternatively:
#!/bin/sh
for num do
sum=$(( sum + num ))
done
printf 'sum is %dn' "$sum"
Instead of constantly shifting the list of command line arguments, this leaves the list untouch and instead iterates over it, adding each one to sum
in turn.
The for num do
loop head may also be written for num in "$@"; do
.
edited May 5 at 19:41
answered May 5 at 18:58
Kusalananda
102k13201317
102k13201317
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f433129%2fissues-with-script-for-adding-numbers%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
Why are you looping across the positional parameters and summing all of them up four times? What happens with your script as written if you only give it three values to add?
â DopeGhoti
Mar 23 at 19:13
1
Why the loop?
sum=$(($1 + $2 + $3 + $4))
is enough IMHO.â nohillside
Mar 23 at 19:14
I suggest you please take a look at this Q/A on AU as you highly need to have a sum function here.
â Ã±ÃÂsýù÷
Mar 23 at 19:22