You can use editors that support graphical interface and you can detect your errors very easily.
Application -> Accessories -> Text Editors
First you have to save your script using .sh extension.
ex :- cal.sh
After that you can type the coding......#This is a simple calculator
option=-1
num1=0
num2=0
echo This is my calculator
echo Availabale operations
echo -e "\tAddition\t\t - 0"
echo -e "\tSubstraction\t\t - 1"
echo -e "\tMultiplication\t\t - 2"
echo -e "\tDivition\t\t - 3"
echo -e "\tFactorial\t\t - 4"
echo -e "\tAny power of any base\t - 5"
echo -n "What is the operation that you want - "
read num
if [ $num -eq 0 ]
then
echo -n "Enter the number 1 - "
read num1
echo -n "Enter the number 2 - "
read num2
echo Answer is `expr $num1 + $num2`
else
if [ $num -eq 1 ]
then
echo -n "Enter the number 1 - "
read num1
echo -n "Enter the number 2 - "
read num2
echo Answer is `expr $num1 - $num2
else
if [ $num -eq 2 ]
then
echo -n "Enter the number 1 - "
read num1
echo -n "Enter the number 2 - "
read num2
echo Answer is `expr $num1 \* $num2`
else
if [ $num -eq 3 ]
then
echo -n "Enter the number 1 - "
read num1
echo -n "Enter the number 2 - "
read num2
echo Answer is `expr $num1 \/ $num2`
echo Remainder is `expr $num1 \% $num2`
else
if [ $num -eq 4 ]
then
echo -n "Enter the number - "
read num1
answer=1
while [ $num1 -gt 0 ]
do
answer=`expr $answer \* $num1`
num1=`expr $num1 - 1`
done
echo "Answer is " $answer
else
if [ $num -eq 5 ]
then
echo -n "Enter the Base - "
read num1
echo -n "Enter the power - "
read num2
answer=1
while [ $num2 -gt 0 ]
do
answer=`expr $answer \* $num1`
num2=`expr $num2 - 1`
done
echo "Answer is " $answer
fi
fi
fi
fi
fi
fi
After finish the coding you have to execute this script. To execute you have to open the terminal first.
Then type sh<space><script name>
ex :- sh cal.sh
It's great,
ReplyDeleteNormally Shell scripting is slightly different
from other programming languages, your example make others to clearly understand the syntax and logic in Shell scripting. Thanks for this
Thayaashangar.
good one....
ReplyDelete