Solved

Simple ksh scripting questions

Posted on 2004-08-21
4
9,520 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 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

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 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

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Expect & sshpass package for Solaris 10 x86 15 922
Understanding curl command 1 54
How to run a Linux Command from a different other than Root in Linux 5 111
Problem to start Neon 20 106
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

777 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