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).










share|improve this question





















  • 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














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).










share|improve this question





















  • 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












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).










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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
















  • 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












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





share|improve this answer






















  • @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










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















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

























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





share|improve this answer






















  • @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














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





share|improve this answer






















  • @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












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





share|improve this answer














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






share|improve this answer














share|improve this answer



share|improve this answer








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
















  • @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

















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3

Museum of Modern and Contemporary Art of Trento and Rovereto