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
adduser(8)						      System Manager's Manual							adduser(8)

NAME
adduser - Adds a new user interactively SYNOPSIS
/usr/sbin/adduser DESCRIPTION
Security Note If you have enhanced security installed on your system, you should use the dxaccounts command to add users. See the Security manual and the dxaccounts(8) reference page for more information. The adduser command is an interactive command for adding new user accounts to your system. The command prompts you for specific informa- tion and informs you of its activity and error conditions. The adduser command invokes /usr/sbin/useradd to add the user account information to /etc/passwd and the hashed password database (if present). Only the superuser can execute this command. The command performs the following tasks: Prompts you to enter a login name for the new user. Enter the login name. If this entry already exists in the passwd file, the command informs you of this and exits. If the entry does not already exist, the command creates one for the new user. Prompts you to enter a UID for the new user. A default UID, which is based on previously existing UID's in the /etc/passwd file, is displayed and can be selected by pressing the Return key. Enter a new UID or accept the default. The command uses this UID in the /etc/passwd file entry for the new user. Note While it is possible to have multiple entries with identical login names and/or identical user id's, it is usually a mistake to do so. Routines that manipulate these files will often return only one of the multiple entries, and that one by random selection. Prompts you for the full name of the new user. Enter the user's full name. This is sometimes called the gecos entry and is dis- played by the finger command. Prompts you to enter a login group for the new user and specifies the default group, users. To accept the default, press the Return key. To select a different group, enter the name of that group, 8 characters or less. If the group does not exist, the command displays a message saying that the group is unknown, lists the names of the groups that are avail- able, and asks whether or not you want to add the new group to the /etc/group file. If you choose the default, the command calls the addgroup command to add the new group to the /etc/group file. The addgroup command queries for the group number of this new group and displays a default value based on existing group numbers in the /etc/group file. Select the default value by pressing Return. Enter a group number or select the default. At this point, the addgroup command ends and the adduser command resumes. There is a limitation on the number of users that can be added to a group. The maximum line length is LINE_MAX as defined in the <limits.h> file. It is recommended that the users be divided into a number of manageable groups. See the System Administration manual for more information on the /etc/group file. Prompts you for the name of other groups for the user to belong to. Again, the group you specify must already exist. If you specify a group to which the user already belongs, the command informs you of this. Prompts you to enter the base directory for the new user and specifies the default directory, /usr/users. To accept the default, press the Return key. To select a different home directory, enter the path of that directory. The path that you specify must exist within a mounted file system. Displays a message that it is adding the new user. At this point, the command makes an entry for the user in the passwd file, creates the home directory, creates the /var/spool/mail directory if necessary, sets ownership and access permissions on the new user's home and mail directories, and copies startup files such as for the new user to the home directory. Asks if you want to edit the authentication file entry for the user. If the environment variable EDITOR is set, adduser will use $EDITOR to edit the user's authentication entries. The adduser command automatically runs the passwd command, which asks you to enter and verify a new password for the user. Note that you cannot use all lowercase letters. If you do, the command asks that you use unusual capitalization and suggests using control characters. If you mistype the password when you are asked to verify it, no password is set and the account is disabled. To enable the user account, enter the passwd command followed by the user name. At this point, the command ends. NOTES
Users are advised to migrate to the /usr/sbin/useradd utility or the dxaccounts utility as this command may be removed in a future version of the operating system. RESTRICTIONS
The addgroup command limits a group name to eight characters or less. If you specify a new group name when prompted, your entry must meet this restriction. EXAMPLES
Enter a login name for the new user (for example, john): chris Enter a UID for (chris) [5006]: Enter a full name for (chris): Chris Ryan Enter a login group for (chris) [users]: Enter another group that (chris) should be a member of. (<Return> for none): Enter a parent directory for (chris) [/usr/users]: The shells are: /bin/sh /bin/csh /bin/ksh Enter a login shell for (chris) [/bin/sh]: Adding new user ... Do you wish to edit the auth file entry for this user (y/[n])? You must enter a new password for (chris). Changing password for chris. New password: Retype new password: FILES
Specifies the command path Group file Password file Default files directory SEE ALSO
Commands: addgroup(8), chfn(1), chsh(1), dxaccounts(8), groupadd(8), mkpasswd(8), passwd(1), removeuser(8), useradd(8), userdel(8), vipw(8) Files: group(4), passwd(4) adduser(8)
All times are GMT -4. The time now is 02:28 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy