Solved

# Simple ksh scripting questions

Posted on 2004-08-21
9,504 Views
Last Modified: 2008-03-17
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
Question by:Mr_Shannon
• 2
4 Comments

LVL 23

Assisted Solution

Mysidia earned 300 total points
ID: 11862662
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

Author Comment

ID: 11862834
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

LVL 5

Accepted Solution

ITcrow earned 200 total points
ID: 11866064

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

LVL 23

Expert Comment

ID: 11866895
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

## Join & Write a Comment Already a member? Login.

### Suggested Solutions

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

#### 706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

#### Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!