Linux Shell Scripting recursive exponentation
up vote
2
down vote
favorite
I am writing a script that takes 2 numbers as an input and uses recursion to power on number to the power of the other, simple exponentiation. However I am new to scripting and cannot figure out where my syntax is errored here.
Here is the script
#!/bin/bash
echo "Enter number: "
read number
echo "Enter power: "
read power
echo "Powering $number to power of $power!"
exp () {
if [ $2 = 1 ]
then
return $1
fi
return $1 * $(exp $1 $2-1 )
}
result=$(exp $number, $power)
echo "Result: $result"
Currently, it kind of freezes, im not sure if I use the parameters correctly (in terms of syntax).
linux bash shell recursion command-line
add a comment |
up vote
2
down vote
favorite
I am writing a script that takes 2 numbers as an input and uses recursion to power on number to the power of the other, simple exponentiation. However I am new to scripting and cannot figure out where my syntax is errored here.
Here is the script
#!/bin/bash
echo "Enter number: "
read number
echo "Enter power: "
read power
echo "Powering $number to power of $power!"
exp () {
if [ $2 = 1 ]
then
return $1
fi
return $1 * $(exp $1 $2-1 )
}
result=$(exp $number, $power)
echo "Result: $result"
Currently, it kind of freezes, im not sure if I use the parameters correctly (in terms of syntax).
linux bash shell recursion command-line
This might help: How to debug a bash script?
– Cyrus
Nov 11 at 11:01
This might help:return
can only return values from 0 to 255.
– Cyrus
Nov 11 at 11:03
@Cyrus That's what I got in bash. In dash, I was able to get 2^30 (but no higher powers of two. Bad idea to use return values in any case.
– PSkocik
Nov 11 at 11:23
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I am writing a script that takes 2 numbers as an input and uses recursion to power on number to the power of the other, simple exponentiation. However I am new to scripting and cannot figure out where my syntax is errored here.
Here is the script
#!/bin/bash
echo "Enter number: "
read number
echo "Enter power: "
read power
echo "Powering $number to power of $power!"
exp () {
if [ $2 = 1 ]
then
return $1
fi
return $1 * $(exp $1 $2-1 )
}
result=$(exp $number, $power)
echo "Result: $result"
Currently, it kind of freezes, im not sure if I use the parameters correctly (in terms of syntax).
linux bash shell recursion command-line
I am writing a script that takes 2 numbers as an input and uses recursion to power on number to the power of the other, simple exponentiation. However I am new to scripting and cannot figure out where my syntax is errored here.
Here is the script
#!/bin/bash
echo "Enter number: "
read number
echo "Enter power: "
read power
echo "Powering $number to power of $power!"
exp () {
if [ $2 = 1 ]
then
return $1
fi
return $1 * $(exp $1 $2-1 )
}
result=$(exp $number, $power)
echo "Result: $result"
Currently, it kind of freezes, im not sure if I use the parameters correctly (in terms of syntax).
linux bash shell recursion command-line
linux bash shell recursion command-line
asked Nov 11 at 10:55
student126
135
135
This might help: How to debug a bash script?
– Cyrus
Nov 11 at 11:01
This might help:return
can only return values from 0 to 255.
– Cyrus
Nov 11 at 11:03
@Cyrus That's what I got in bash. In dash, I was able to get 2^30 (but no higher powers of two. Bad idea to use return values in any case.
– PSkocik
Nov 11 at 11:23
add a comment |
This might help: How to debug a bash script?
– Cyrus
Nov 11 at 11:01
This might help:return
can only return values from 0 to 255.
– Cyrus
Nov 11 at 11:03
@Cyrus That's what I got in bash. In dash, I was able to get 2^30 (but no higher powers of two. Bad idea to use return values in any case.
– PSkocik
Nov 11 at 11:23
This might help: How to debug a bash script?
– Cyrus
Nov 11 at 11:01
This might help: How to debug a bash script?
– Cyrus
Nov 11 at 11:01
This might help:
return
can only return values from 0 to 255.– Cyrus
Nov 11 at 11:03
This might help:
return
can only return values from 0 to 255.– Cyrus
Nov 11 at 11:03
@Cyrus That's what I got in bash. In dash, I was able to get 2^30 (but no higher powers of two. Bad idea to use return values in any case.
– PSkocik
Nov 11 at 11:23
@Cyrus That's what I got in bash. In dash, I was able to get 2^30 (but no higher powers of two. Bad idea to use return values in any case.
– PSkocik
Nov 11 at 11:23
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
You need $(( ))
to force arithmetic evaluation. Then you can do it with return values:
number=2 power=7
exp () {
if [ $2 -eq 1 ]; then return $1; fi
exp $1 $(($2-1))
return $(($1 * $?))
}
exp $number $power; result=$?
echo "Result: $result"
but it's a bad idea, because shells kind of reserve nonzero return values to communicate failure (e.g. the above solution
will "break" set -e
).
More idiomatically, you can use stdout:
set -e
number=2 power=7
exp () {
if [ $2 -eq 1 ]; then echo $1; return; fi
echo $(($1 * $(exp $1 $(($2-1)) ) ))
}
result=$(exp $number $power)
echo "Result: $result"
but that's kind of inefficient with all the subshells.
Best to avoid the recursion and simply loop:
number=2 power=7
exp () {
local res=1 i=0;
while [ $i -lt $2 ]; do res=$((res*$1)); i=$((i+1)); done
echo $res
}
exp $number $power
@student126: Please take a look at: [What should I do when someone answers my question?](stackoverflow.com/help/someone-answers
– Cyrus
Nov 11 at 11:48
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
You need $(( ))
to force arithmetic evaluation. Then you can do it with return values:
number=2 power=7
exp () {
if [ $2 -eq 1 ]; then return $1; fi
exp $1 $(($2-1))
return $(($1 * $?))
}
exp $number $power; result=$?
echo "Result: $result"
but it's a bad idea, because shells kind of reserve nonzero return values to communicate failure (e.g. the above solution
will "break" set -e
).
More idiomatically, you can use stdout:
set -e
number=2 power=7
exp () {
if [ $2 -eq 1 ]; then echo $1; return; fi
echo $(($1 * $(exp $1 $(($2-1)) ) ))
}
result=$(exp $number $power)
echo "Result: $result"
but that's kind of inefficient with all the subshells.
Best to avoid the recursion and simply loop:
number=2 power=7
exp () {
local res=1 i=0;
while [ $i -lt $2 ]; do res=$((res*$1)); i=$((i+1)); done
echo $res
}
exp $number $power
@student126: Please take a look at: [What should I do when someone answers my question?](stackoverflow.com/help/someone-answers
– Cyrus
Nov 11 at 11:48
add a comment |
up vote
1
down vote
accepted
You need $(( ))
to force arithmetic evaluation. Then you can do it with return values:
number=2 power=7
exp () {
if [ $2 -eq 1 ]; then return $1; fi
exp $1 $(($2-1))
return $(($1 * $?))
}
exp $number $power; result=$?
echo "Result: $result"
but it's a bad idea, because shells kind of reserve nonzero return values to communicate failure (e.g. the above solution
will "break" set -e
).
More idiomatically, you can use stdout:
set -e
number=2 power=7
exp () {
if [ $2 -eq 1 ]; then echo $1; return; fi
echo $(($1 * $(exp $1 $(($2-1)) ) ))
}
result=$(exp $number $power)
echo "Result: $result"
but that's kind of inefficient with all the subshells.
Best to avoid the recursion and simply loop:
number=2 power=7
exp () {
local res=1 i=0;
while [ $i -lt $2 ]; do res=$((res*$1)); i=$((i+1)); done
echo $res
}
exp $number $power
@student126: Please take a look at: [What should I do when someone answers my question?](stackoverflow.com/help/someone-answers
– Cyrus
Nov 11 at 11:48
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
You need $(( ))
to force arithmetic evaluation. Then you can do it with return values:
number=2 power=7
exp () {
if [ $2 -eq 1 ]; then return $1; fi
exp $1 $(($2-1))
return $(($1 * $?))
}
exp $number $power; result=$?
echo "Result: $result"
but it's a bad idea, because shells kind of reserve nonzero return values to communicate failure (e.g. the above solution
will "break" set -e
).
More idiomatically, you can use stdout:
set -e
number=2 power=7
exp () {
if [ $2 -eq 1 ]; then echo $1; return; fi
echo $(($1 * $(exp $1 $(($2-1)) ) ))
}
result=$(exp $number $power)
echo "Result: $result"
but that's kind of inefficient with all the subshells.
Best to avoid the recursion and simply loop:
number=2 power=7
exp () {
local res=1 i=0;
while [ $i -lt $2 ]; do res=$((res*$1)); i=$((i+1)); done
echo $res
}
exp $number $power
You need $(( ))
to force arithmetic evaluation. Then you can do it with return values:
number=2 power=7
exp () {
if [ $2 -eq 1 ]; then return $1; fi
exp $1 $(($2-1))
return $(($1 * $?))
}
exp $number $power; result=$?
echo "Result: $result"
but it's a bad idea, because shells kind of reserve nonzero return values to communicate failure (e.g. the above solution
will "break" set -e
).
More idiomatically, you can use stdout:
set -e
number=2 power=7
exp () {
if [ $2 -eq 1 ]; then echo $1; return; fi
echo $(($1 * $(exp $1 $(($2-1)) ) ))
}
result=$(exp $number $power)
echo "Result: $result"
but that's kind of inefficient with all the subshells.
Best to avoid the recursion and simply loop:
number=2 power=7
exp () {
local res=1 i=0;
while [ $i -lt $2 ]; do res=$((res*$1)); i=$((i+1)); done
echo $res
}
exp $number $power
edited Nov 12 at 19:06
answered Nov 11 at 11:20
PSkocik
31.7k54569
31.7k54569
@student126: Please take a look at: [What should I do when someone answers my question?](stackoverflow.com/help/someone-answers
– Cyrus
Nov 11 at 11:48
add a comment |
@student126: Please take a look at: [What should I do when someone answers my question?](stackoverflow.com/help/someone-answers
– Cyrus
Nov 11 at 11:48
@student126: Please take a look at: [What should I do when someone answers my question?](stackoverflow.com/help/someone-answers
– Cyrus
Nov 11 at 11:48
@student126: Please take a look at: [What should I do when someone answers my question?](stackoverflow.com/help/someone-answers
– Cyrus
Nov 11 at 11:48
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2fstackoverflow.com%2fquestions%2f53248017%2flinux-shell-scripting-recursive-exponentation%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
This might help: How to debug a bash script?
– Cyrus
Nov 11 at 11:01
This might help:
return
can only return values from 0 to 255.– Cyrus
Nov 11 at 11:03
@Cyrus That's what I got in bash. In dash, I was able to get 2^30 (but no higher powers of two. Bad idea to use return values in any case.
– PSkocik
Nov 11 at 11:23