Sponsored Content
Top Forums Shell Programming and Scripting Simple maths calculator loop. Post 302583043 by johnthebaptist on Monday 19th of December 2011 11:17:11 AM
Old 12-19-2011
Simple maths calculator loop.

Hi, I am trying to make a maths calculator that:

1. Prompts the user for a number.
2. Prompts the user for an operation (add, subtract, divide or multiply)
3. Prompts the user for a number.
4. Prompts the user for another operation (same as above) OR the option to get the result for the equation.
5. The script should loop back if the user chooses to add another operation, and ask for another number (repeat until result)

Now, i already have the arithmetic sorted, and i know i have to add an UNTIL loop, but i'm not very good with loops as i find them hard to understand.

Here is a copy of my code so far:

-----
Code:
echo "please enter a number"
read n1

echo "please choose an operation"
echo "1. add"
echo "2. subtract"
echo "3. divide"
echo "4. multiply"
read opr

echo "please enter a number again"
read n2

if [ $opr = "1" ]
   then
      echo $((n1+n2))
elif [ $opr = "2" ]
   then  
      echo $((n1-n2))
elif [ $opr = "3" ]
   then
      echo $((n1/n2))
elif [ $opr = "4" ]
   then
       echo $((n1*n2))

fi
exit 0

-----

Can anyone give me any pointers? I really have tried to look for solutions but this is my last hope. ANTHING WILL HELP. THanks

Moderator's Comments:
Mod Comment How to use code tags when posting data and code samples.

Last edited by Franklin52; 12-19-2011 at 12:56 PM.. Reason: Please use code tags for data and code samples, thank you
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem with Maths

Heres a script i wrote as a bit of practise. What it does is insert a line in the middle of a file. The line being $1 and the file being $2 #!/bin/bash rm tempfile touch tempfile count=1 linenum= `wc -l < $2` if then echo $1 >> $2 else even=`expr "$linenum" % 2` if then... (3 Replies)
Discussion started by: Quesa
3 Replies

2. Shell Programming and Scripting

a simple while loop

Hallo everyone I might just be being dumb, but I am using the BASH shell and cannot get the following script to work: x=0 while do echo $x x=´echo "$x + 1" | bc´ done Can anybody help me out. I am just get a repeating output saying: bc: command not found 0 + 1: command not... (5 Replies)
Discussion started by: syno
5 Replies

3. Shell Programming and Scripting

simple for loop

i have the following process running in background: when i give "ps -lef" ------------------------------------------------------------------------ user2 user1 user1 user3 user1 user4 user5 user4 user3 user4 user2 user1 user1 user3 user1 user4 (3 Replies)
Discussion started by: ali560045
3 Replies

4. Shell Programming and Scripting

Simple using For loop

Hi expert, I'm using csh Code: #!/bin/csh set x = 0 set number = `awk '{array=$0} END {print array;}'` i want to use for loop to store data to $number repeatly untill x = 23 How to use c shell for loop? (2 Replies)
Discussion started by: vincyoxy
2 Replies

5. UNIX for Dummies Questions & Answers

Simple loop

I need to chmod a bunch of files with a specific extension in one directory. If I understand correctly first I would run ls command like this ls -R | grep .mp3 > /tmp/list once I have the output file I should be able to run a loop to chmod all the files in the list created. This is where... (5 Replies)
Discussion started by: eugenes18t
5 Replies

6. Shell Programming and Scripting

Maths with variables

Hello, I'm trying to write a while loop for a decimal value in tcsh which I know can't be done. Instead I want my increments to be one order of magnitude too large and then divide it by 10 when I use the variable. However, I don't know how to divide my variable and set it as another. set... (1 Reply)
Discussion started by: DFr0st
1 Replies

7. Shell Programming and Scripting

a simple loop

Does any body can help me with a loop in this example? if then if then runner=$(grep "$1" "$2") runne=$(grep "$1" "$3") run=$(grep "$1" "$4") fi fi # # Message on screen... (3 Replies)
Discussion started by: bartsimpsong
3 Replies

8. Shell Programming and Scripting

Simple calculator with menu input - Need Help

I am trying to make a calculator. The user Enters number 1, chooses and operation, enters number 2, then chooses another operation or for the answer to be displayed. eg. 1 + 1 = or 1 + 1 + 2 + 1 = Both of these should be possible. #!/bin/bash read -p "what's the first number? " n1... (3 Replies)
Discussion started by: redshine6
3 Replies

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

10. Shell Programming and Scripting

Simple Scientific calculator for ADE, the UNIX environment for the AMIGA A1200(HD).

ADE is a UNIX environment for the ancient AMIGA A1200. By default this does NOT have the 'bc' command line calculator. Although I did a DEMO code to create a C source and compile it under python 1.4.0 and ADE using ksh88 and the gcc of the day, I decided to create this baby that requires no Python... (2 Replies)
Discussion started by: wisecracker
2 Replies
newkey(1M)                                                System Administration Commands                                                newkey(1M)

NAME
newkey - create a new Diffie-Hellman key pair in the publickey database SYNOPSIS
newkey -h hostname [-s nisplus | nis | files | ldap] newkey -u username [-s nisplus | nis | files | ldap] DESCRIPTION
newkey establishes new public keys for users and machines on the network. These keys are needed when using secure RPC or secure NFS ser- vice. newkey prompts for a password for the given username or hostname and then creates a new public/secret Diffie-Hellman 192 bit key pair for the user or host. The secret key is encrypted with the given password. The key pair can be stored in the /etc/publickey file, the NIS pub- lickey map, or the NIS+ cred.org_dir table. newkey consults the publickey entry in the name service switch configuration file (see nsswitch.conf(4)) to determine which naming service is used to store the secure RPC keys. If the publickey entry specifies a unique name service, newkey will add the key in the specified name service. However, if there are multiple name services listed, newkey cannot decide which source to update and will display an error mes- sage. The user is required to specify the source explicitly with the -s option. In the case of NIS, newkey should be run by the superuser on the master NIS server for that domain. In the case of NIS+, newkey should be run by the superuser on a machine which has permission to update the cred.org_dir table of the new user/host domain. In the case of NIS+, nisaddcred(1M) should be used to add new keys. newkey cannot be used to create keys other than 192-bit Diffie-Hellman. In the case of LDAP, newkey should be run by the superuser on a machine that also recognizes the directory manager's bind distinguished name (DN) and password to perform an LDAP update for the host. OPTIONS
-h hostname Create a new public/secret key pair for the privileged user at the given hostname. Prompts for a password for the given hostname. -u username Create a new public/secret key pair for the given username. Prompts for a password for the given username. -s nisplus Update the database in the specified source: nisplus (for NIS+), nis (for NIS), files, or ldap (LDAP). Other sources may be -s nis available in the future. -s files -s ldap ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWcsu | +-----------------------------+-----------------------------+ SEE ALSO
chkey(1), keylogin(1), nisaddcred(1M), nisclient(1M), nsswitch.conf(4), publickey(4), attributes(5) NOTES
NIS+ might not be supported in future releases of the SolarisTM Operating Environment. Tools to aid the migration from NIS+ to LDAP are available in the Solaris 9 operating environment. For more information, visit http://www.sun.com/directory/nisplus/transition.html. SunOS 5.10 13 Nov 2003 newkey(1M)
All times are GMT -4. The time now is 03:45 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy