30 November 2012

Shell Script to find "a" to the power "b" using function

1 comment

Q. How do I write Shell Script function to find "a" to the power "b"


Ans.

#!/bin/bash
#function to find "a" to the power "b"
power()
{
num=$1
pow=$2
counter=1
result=1
if((pow==0)); then
result=1
fi
if ((num==0)); then
result=0
fi
if((num>=1&&pow>=1)); then
while((counter<=pow))
do
result=$((result*num))
counter=$((counter + 1))
done
fi
#Printing the result
echo "$1 to the power $2 is $result"
}

#main script
read -p "Enter number:" num
read -p "Enter power:" pow

#calling above function
power $num $pow

Output


shell script using function

Enjoy :)


If You Liked This Post Please Take a Time To Share This Post

You May Also Like...

1 comment: