Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Simple ksh scripting questions

Posted on 2004-08-21
4
Medium Priority
?
9,583 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
Comment
Question by:Mr_Shannon
  • 2
4 Comments
 
LVL 23

Assisted Solution

by:Mysidia
Mysidia earned 1200 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

by:Mr_Shannon
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

by:
ITcrow earned 800 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

by:Mysidia
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
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…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Suggested Courses

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

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

Join & Ask a Question