Unix addition script
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
Im currently trying to make a addition command in unix and have come up with the following code:
#! /bin/bash
#! Add - adds two given numbers together and displays the result
"$num1" = $1
"$num2" = $2
echo "Enter two numbers"
read num1 num2
sum=$(“$num1” + “$num2”)
echo "The sum is = $sum"
This however does not work.
shell-script command
add a comment |
up vote
0
down vote
favorite
Im currently trying to make a addition command in unix and have come up with the following code:
#! /bin/bash
#! Add - adds two given numbers together and displays the result
"$num1" = $1
"$num2" = $2
echo "Enter two numbers"
read num1 num2
sum=$(“$num1” + “$num2”)
echo "The sum is = $sum"
This however does not work.
shell-script command
1
Those look like "smart quotes" which wouldn't work if that's accurate. Aside from that what about it "does not work"?
– Eric Renouf
Nov 25 '15 at 19:38
When i first run the command it displays ./add: line 5: : command not found ./add: line 6: : command not found Enter two numbers and when i add the two numbers it displays 2 2 ./add: line 10: “2”: command not found The sum is =
– S.Jones
Nov 25 '15 at 19:46
1
num1=$1
. No spaces, and the undecorated name on the left-hand side of the equal sign. Of course, those assignments are unnecessary, because you overwrite their values with the `read1 statement before you ever use them.
– chepner
Nov 25 '15 at 21:14
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Im currently trying to make a addition command in unix and have come up with the following code:
#! /bin/bash
#! Add - adds two given numbers together and displays the result
"$num1" = $1
"$num2" = $2
echo "Enter two numbers"
read num1 num2
sum=$(“$num1” + “$num2”)
echo "The sum is = $sum"
This however does not work.
shell-script command
Im currently trying to make a addition command in unix and have come up with the following code:
#! /bin/bash
#! Add - adds two given numbers together and displays the result
"$num1" = $1
"$num2" = $2
echo "Enter two numbers"
read num1 num2
sum=$(“$num1” + “$num2”)
echo "The sum is = $sum"
This however does not work.
shell-script command
shell-script command
edited Nov 17 at 20:48
Rui F Ribeiro
38.2k1475123
38.2k1475123
asked Nov 25 '15 at 19:35
S.Jones
483
483
1
Those look like "smart quotes" which wouldn't work if that's accurate. Aside from that what about it "does not work"?
– Eric Renouf
Nov 25 '15 at 19:38
When i first run the command it displays ./add: line 5: : command not found ./add: line 6: : command not found Enter two numbers and when i add the two numbers it displays 2 2 ./add: line 10: “2”: command not found The sum is =
– S.Jones
Nov 25 '15 at 19:46
1
num1=$1
. No spaces, and the undecorated name on the left-hand side of the equal sign. Of course, those assignments are unnecessary, because you overwrite their values with the `read1 statement before you ever use them.
– chepner
Nov 25 '15 at 21:14
add a comment |
1
Those look like "smart quotes" which wouldn't work if that's accurate. Aside from that what about it "does not work"?
– Eric Renouf
Nov 25 '15 at 19:38
When i first run the command it displays ./add: line 5: : command not found ./add: line 6: : command not found Enter two numbers and when i add the two numbers it displays 2 2 ./add: line 10: “2”: command not found The sum is =
– S.Jones
Nov 25 '15 at 19:46
1
num1=$1
. No spaces, and the undecorated name on the left-hand side of the equal sign. Of course, those assignments are unnecessary, because you overwrite their values with the `read1 statement before you ever use them.
– chepner
Nov 25 '15 at 21:14
1
1
Those look like "smart quotes" which wouldn't work if that's accurate. Aside from that what about it "does not work"?
– Eric Renouf
Nov 25 '15 at 19:38
Those look like "smart quotes" which wouldn't work if that's accurate. Aside from that what about it "does not work"?
– Eric Renouf
Nov 25 '15 at 19:38
When i first run the command it displays ./add: line 5: : command not found ./add: line 6: : command not found Enter two numbers and when i add the two numbers it displays 2 2 ./add: line 10: “2”: command not found The sum is =
– S.Jones
Nov 25 '15 at 19:46
When i first run the command it displays ./add: line 5: : command not found ./add: line 6: : command not found Enter two numbers and when i add the two numbers it displays 2 2 ./add: line 10: “2”: command not found The sum is =
– S.Jones
Nov 25 '15 at 19:46
1
1
num1=$1
. No spaces, and the undecorated name on the left-hand side of the equal sign. Of course, those assignments are unnecessary, because you overwrite their values with the `read1 statement before you ever use them.– chepner
Nov 25 '15 at 21:14
num1=$1
. No spaces, and the undecorated name on the left-hand side of the equal sign. Of course, those assignments are unnecessary, because you overwrite their values with the `read1 statement before you ever use them.– chepner
Nov 25 '15 at 21:14
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
((...))
is the way to do arithmetic, not single parens, and you don't need quotes there Try:
sum=$((num1+num2))
add a comment |
up vote
0
down vote
Ignoring the syntax errors in the script, it looks like the two numbers are given, i.e. they are present on the script's command line.
That means that the script could be reduced to
#!/bin/sh
printf 'The sum of %d and %d is %dn' "$1" "$2" "$(( $1 + $2 ))"
This obviously does no verification of the passed arguments whatsoever. For example, it does not verify that there are exactly two arguments, and it also does not verify that they are decimal integers.
The script would be used as
$ ./script.sh -23 32
The sum of -23 and 32 is 9
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
((...))
is the way to do arithmetic, not single parens, and you don't need quotes there Try:
sum=$((num1+num2))
add a comment |
up vote
2
down vote
accepted
((...))
is the way to do arithmetic, not single parens, and you don't need quotes there Try:
sum=$((num1+num2))
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
((...))
is the way to do arithmetic, not single parens, and you don't need quotes there Try:
sum=$((num1+num2))
((...))
is the way to do arithmetic, not single parens, and you don't need quotes there Try:
sum=$((num1+num2))
answered Nov 25 '15 at 19:44
Eric Renouf
13.2k42949
13.2k42949
add a comment |
add a comment |
up vote
0
down vote
Ignoring the syntax errors in the script, it looks like the two numbers are given, i.e. they are present on the script's command line.
That means that the script could be reduced to
#!/bin/sh
printf 'The sum of %d and %d is %dn' "$1" "$2" "$(( $1 + $2 ))"
This obviously does no verification of the passed arguments whatsoever. For example, it does not verify that there are exactly two arguments, and it also does not verify that they are decimal integers.
The script would be used as
$ ./script.sh -23 32
The sum of -23 and 32 is 9
add a comment |
up vote
0
down vote
Ignoring the syntax errors in the script, it looks like the two numbers are given, i.e. they are present on the script's command line.
That means that the script could be reduced to
#!/bin/sh
printf 'The sum of %d and %d is %dn' "$1" "$2" "$(( $1 + $2 ))"
This obviously does no verification of the passed arguments whatsoever. For example, it does not verify that there are exactly two arguments, and it also does not verify that they are decimal integers.
The script would be used as
$ ./script.sh -23 32
The sum of -23 and 32 is 9
add a comment |
up vote
0
down vote
up vote
0
down vote
Ignoring the syntax errors in the script, it looks like the two numbers are given, i.e. they are present on the script's command line.
That means that the script could be reduced to
#!/bin/sh
printf 'The sum of %d and %d is %dn' "$1" "$2" "$(( $1 + $2 ))"
This obviously does no verification of the passed arguments whatsoever. For example, it does not verify that there are exactly two arguments, and it also does not verify that they are decimal integers.
The script would be used as
$ ./script.sh -23 32
The sum of -23 and 32 is 9
Ignoring the syntax errors in the script, it looks like the two numbers are given, i.e. they are present on the script's command line.
That means that the script could be reduced to
#!/bin/sh
printf 'The sum of %d and %d is %dn' "$1" "$2" "$(( $1 + $2 ))"
This obviously does no verification of the passed arguments whatsoever. For example, it does not verify that there are exactly two arguments, and it also does not verify that they are decimal integers.
The script would be used as
$ ./script.sh -23 32
The sum of -23 and 32 is 9
answered Nov 17 at 20:54
Kusalananda
116k15218352
116k15218352
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
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f245498%2funix-addition-script%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Those look like "smart quotes" which wouldn't work if that's accurate. Aside from that what about it "does not work"?
– Eric Renouf
Nov 25 '15 at 19:38
When i first run the command it displays ./add: line 5: : command not found ./add: line 6: : command not found Enter two numbers and when i add the two numbers it displays 2 2 ./add: line 10: “2”: command not found The sum is =
– S.Jones
Nov 25 '15 at 19:46
1
num1=$1
. No spaces, and the undecorated name on the left-hand side of the equal sign. Of course, those assignments are unnecessary, because you overwrite their values with the `read1 statement before you ever use them.– chepner
Nov 25 '15 at 21:14