Sponsored Content
Homework and Emergencies Homework & Coursework Questions Creating a calculator with condition Post 303007717 by Scott on Monday 20th of November 2017 04:10:17 PM
Old 11-20-2017
I would think the simplest way to handle this would be to add an additional condition inside your pre-existing condition (that is not related to Obama Care Smilie) to test if "n2" is 0 (zero) before you attempt to divide by it:

Code:
elif [ $opr = "4" ]
   then
     ... test if n2 is zero, if not, do this...
       echo $((n1/n2))
    ... else do this
fi

e.g.
Code:
...
elif [ $opr = "4" ]
   then
     if [ "$n2" != 0 ]; then # for numbers, normally, you would use "-ne" (not equal to) in place of "!=",  but you didn't sanitise the input
       echo $((n1/n2))
     else
       echo "Divide by zero"
     fi
fi
...

If the intention is not to show that you are dividing by zero, but to prevent it doing so altogether, you can amend the existing elif that handles that case to also check that "n2" is not zero. Talking of which, it may be better (simpler) to use a "case" statement instead of "if" - ifs can get a bit clumsy looking, when there's lots of elifs going on.
This User Gave Thanks to Scott For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

calculator program..

Hey can anyone tell me the korn script code to implement an interactive integer calculator using the shell's built in arithemetic expression evaluation (2 Replies)
Discussion started by: sahithi_khushi
2 Replies

2. Shell Programming and Scripting

Calculator

I am pretty new to the Unix word, and have created a working calculator script. I have one problem. It doesn't use any decimals, it rounds off to the nearest whole number. 1 #!/bin/ksh 2 while true; do 3 echo -n "Enter the first integer: "; read IN1 4 test... (2 Replies)
Discussion started by: ironhead3fan
2 Replies

3. UNIX for Dummies Questions & Answers

calculator

hi, im new to the unix system and scripting and was wondering if anyone could help me with this problem iv been havin... i want the system to: 1. ask me for a number 2. ask me for a command to use on that number (* + - /) 3. ask me for another number 4. then ask me for another command, if the... (2 Replies)
Discussion started by: jdougy
2 Replies

4. Shell Programming and Scripting

redirecting output and creating condition for while loop.

I have 2 questions. 1) Is there a means of directing output to a file (">") while making it still output to the console? I have a script that calls another lengthy script. 2) I can direct the output of the lengthy script and grep it for the words "good" or "bad" to know if I need the run... (2 Replies)
Discussion started by: mrwatkin
2 Replies

5. Shell Programming and Scripting

Help with calculator code

Hi Guys, I found this code in net.. it is working fine.. But can anybody explain me the sed statement used in the code.. echo "Enter the expression:\c" read express eval echo "$express"|sed 's/^/'$precision' \ /'|bc -l|\ sed -n '1,${ /syntax/!{ } ... (2 Replies)
Discussion started by: mac4rfree
2 Replies

6. Shell Programming and Scripting

redirect stdout echo command in condition A run in condition B

hi, I have some problems in my simple script about the redirect echo stdout command inside a condition. Why is the echo command inside the elif still execute in the else command Here are my simple script After check on the two diff output the echo stdout redirect is present in two diff... (3 Replies)
Discussion started by: jao_madn
3 Replies

7. Homework & Coursework Questions

Simple Calculator

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known/data: Script a simple calculator. In the command line enter the script file /home/etc/mycalc or /home/etc/mycalc 1 +... (6 Replies)
Discussion started by: herb bertz
6 Replies

8. Shell Programming and Scripting

If condition return 0 even when it fails to satisfy te condition

HI My doubt may be basic one but I need to get it clarified.. When i use "if" condition that checks for many AND, OR logical conditions like if ]; then return 0 fi Even the if condition fails it returns as zero.. Any clue.. But if i add else condition like if ]; ... (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

9. Shell Programming and Scripting

Creating a condition on a bash script

I wrote a code to find codons in a DNA string. The only problem I have is how do I make the code only work for a file with DNA. This means the file only has the characters a,c,g,t and no white space characters. (3 Replies)
Discussion started by: germany1517
3 Replies

10. UNIX for Beginners Questions & Answers

Creating a calculator with condition

So have I got this : #!/bin/bash clear echo "Enter the first number:" read n1 echo "Choose an operation:" echo "1. add" echo "2. subtract" echo "3. multiply" echo "4. divide" read opr echo "Enter the second number:" read n2 (1 Reply)
Discussion started by: mugiboya
1 Replies
XMLIF(1)							       xmlif								  XMLIF(1)

NAME
xmlif - conditional processing instructions for XML SYNOPSIS
xmlif [attrib=value...] DESCRIPTION
xmlif filters XML according to conditionalizing markup. This can be useful for formatting one of several versions of an XML document depending on conditions passed to the command. Attribute/value pairs from the command line are matched against the attributes associated with certain processing instructions in the document. The instructions are <?xmlif if?> and its inverse <?xmlif if not?>, <?xmlif elif?> and its inverse <?xmlif elif not?>, <?xmlif else?>, and <?xmlif fi?>. Argument/value pairs given on the command line are checked against the value of corresponding attributes in the conditional processing instructions. An `attribute match' happens if an attribute occurs in both the command-line arguments and the tag, and the values match. An `attribute mismatch' happens if an attribute occurs in both the command-line arguments and the tag, but the values do not match. Spans between <?xmlif if?> or <?xmlif elif?> and the next conditional processing instruction at the same nesting level are passed through unaltered if there is at least one attribute match and no attribute mismatch; spans between <?xmlif if not?> and <?xmlif elif not?> and the next conditional processing instruction are passed otherwise. Spans between <?xmlif else?> and the next conditional-processing tag are passed through only if no previous span at the same level has been passed through. <?xmlif if?> and <?xmlif fi?> (and their `not' variants) change the current nesting level; <?xmlif else?> and <?xmlif elif?> do not. All these processing instructions will be removed from the output produced. Aside from the conditionalization, all other input is passed through untouched; in particular, entity references are not resolved. Value matching is by string equality, except that "|" in an attribute value is interpreted as an alternation character. Thus, saying foo='red|blue' on the command line enables conditions red and blue. Saying color='black|white' in a tag matches command-line conditions color='black' and color='white'. Here is an example: Always issue this text. <?xmlif if condition='html'?> Issue this text if 'condition=html' is given on the command line. <?xmlif elif condition='pdf|ps'?> Issue this text if 'condition=pdf' or 'condition=ps' is given on the command line. <?xmlif else?> Otherwise issue this text. <?xmlif fi?> Always issue this text. AUTHOR
Eric S. Raymond <esr@snark.thyrsus.com> Author of xmlif program Linux April 2009 XMLIF(1)
All times are GMT -4. The time now is 05:33 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy