Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 9592
  • Last Modified:

Simple ksh scripting questions

Here is what I need to do:
Create script which will allow the user to choose from options to do simple math calculations
1.      Make sure the user cannot divide by zero. Have the user repeat entering the second number if this happens.
2.      If a number or character other than those in the listed choices is entered at the menu, prompt the user to fix the mistake and try again.
3.      Catch bad input from the menu
4.      Loop the script until the exit choice is chosen

Here is my script:

#!/bin/ksh
until [ $input -eq 5 ] ;
do
echo "1. Add Two Numbers"
echo "2. Subtract Two Numbers"
echo "3. Multiply Two Numbers"
echo "4. Divide Two Numbers"
echo "5. Exit"

read input

if [ $input -eq 1 ] ;
then echo "Input your first number to be added"
read a
echo "Input your second number to be added"
read b
echo "The sum of $a and $b equals `expr $a + $b` \n"

elif [ $input -eq 2 ]
then echo "Input your first number to be subtracted"
read a
echo "Input your second number to be subtracted"
read b
echo "$a minus $b equals `expr $a - $b` \n"

elif [ $input -eq 3 ]
then echo "Input your first number to be multiplied"
read a
echo "Input your second number to be multiplied"
read b
echo "$a multiplied by $b equals `expr $a \* $b` \n"

elif [ $input -eq 4 ]
then echo "Input your first number to be divided"
read a
echo "Input your second number to be divided"
read b
echo "$a divided by $b equals `expr $a / $b` with a remainder of `expr $a % $b` \n"

elif [ $input -eq 5 ]
then echo "EXIT"

else
echo “Errof Fool\nPlease try again\n”
fi
done

First question, why doesn't \n create a newline?
Second question, why do I get this error when I start the script and how do I get it to stop? ./shannonscript[3]: [: argument expected
Third question, how can I make the division not divide by 0?
Fourth question, how can I catch errors in the math portion (user inputs a character instead of a number)?
0
Mr_Shannon
Asked:
Mr_Shannon
  • 2
2 Solutions
 
MysidiaCommented:
re: first question
try using echo's -e option
echo -e "Blah\n"

re: second question,  $input   is read in that until test  before it is given any value
so there's seen a missing argument

re: third question
check the value read in isn't equal to it just before performing the computation...

re: fourth question.. you could use a case statement on the input to match the first character
with a case item like:  [0123456789]*)    ;;

to validate the whole entry.. use pattern matching...
i.e.    expr   $whatever : '\([0-9]\+\)$'

0
 
Mr_ShannonAuthor Commented:
1st echo -e doesn't seem to work

2nd is there anyway to get rid of the first arguemnt

3rd - the rest I don't understand
0
 
ITcrowCommented:

First question, why doesn't \n create a newline?

Your echo statements should work fine. Otherwise to give effect of newline
you can add "echo" in the following line.

Eg. echo "Hello\n"
is equivalent to:
echo Hello
echo

Second question, why do I get this error when I start the script and how do I get it to stop? ./shannonscript[3]: [: argument expected
Initialize 'input' to some value.
Eg. add "input=0" without quotes just above until ...

Third question, how can I make the division not divide by 0?
Check "$b=0" and branch out, if it's true.

Fourth question, how can I catch errors in the math portion (user inputs a character instead of a number)?
Check $a="[0-9]+"  and $b="[0-9]+"  if both are true branch out. Once you understand this part, they
add a regular expression which takes care of numbers in genral.

man ksh will help in understanding the comparison and logical branching out of code.

0
 
MysidiaCommented:
re: stopping division by zero
For one possibility ... just as you've done to test the input

if [ "$b" -ne 0 ] ; then
       actions to take if b is not zero
else
       actions to take if zero
fi
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now