Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
11-26-2007, 08:54 PM
|
#1
|
LQ Newbie
Registered: Nov 2007
Location: OK
Distribution: Fedora 4 -I think
Posts: 7
Rep:
|
Simple Shell Script Calculator
Hello,
I'm writing a simple calculator script and I'm having issues getting it to run correctly. Here is my code:
(
while [ $ != 'x' ]
do
echo "Welcome to calculator (x to quit)"
echo "Enter the first operand: "
read value1
echo "Enter an operator (+, -, *, /): "
read operator
echo "Enter the second operand: "
read value2
if [ " $ operator " = " + " ] ; then
answer=$(echo "scale=2;value1+value2" |bc);
elif [ " $ operator " = " - " ] ; then
answer=$(echo " scale=2;value1-value2" |bc);
elif [ " $ operator " = " / " ] ; then
answer=$(echo "scale=2;value1/value2" |bc);
elif [ " $ operator " = " * " ] ; then
answer=$(echo "scale=2;value1*value2" |bc);
elif [ " $ operator " = " % " ] ; then
answer=$(echo "scale=2;value1%value2" |bc);
fi
echo "Answer: $value1 $operator $value2 = $answer";
done
)
As it is right now it just loops through asking for the first operand, the operator, and the second operand. I have rearranged it several ways and still haven't gotten anywhere. Thanks for any help you guys can offer.
|
|
|
11-26-2007, 09:16 PM
|
#2
|
Senior Member
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113
Rep: 
|
If this is homework like your other question was, we really shouldn't be helping you out with it.
|
|
|
11-26-2007, 09:51 PM
|
#3
|
LQ Newbie
Registered: Nov 2007
Location: OK
Distribution: Fedora 4 -I think
Posts: 7
Original Poster
Rep:
|
Well you don't have to give me the answer but some suggestions would be great. I have read the man pages, various tutorials, etc. It's not like I'm sitting back and asking anyone to do it for me. And on the other one I still have to work on it it because the solution provided didn't fix it. Furthermore, on this one I have written all of it, I just can't get it to do what it should because obviously it is not in the right order or something. So, I guess it's up to you guys, thought that was what you guys are here for.
|
|
|
11-26-2007, 10:27 PM
|
#4
|
Senior Member
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113
Rep: 
|
Using [ code ] tags and indenting is good for clarity. The red '$' seems to be missing a value, but you need to test for it after assigning to it - if someone types 'x' in place of $value1, you should exit. A minimal example in green. Though you could just say 'ctrl-c to exit' and not write any code for it. The red stuff is wrong due to spacing. Most are not needed and '$ operator' is wrong. You'd be better off with a case statement instead of the if with all the elifs. bc doesn't really need a scale without 'bc -l'. The interface is odd, in that you could simply slurp in a single expression instead of getting it in three stages. Etc. Hope some of that helps.
Code:
while [$ != 'x' ]; do
echo "Welcome to calculator (x to quit)"
echo "Enter the first operand: "
read value1
if [[ $value1 == x ]]; then exit; fi
echo "Enter an operator (+, -, *, /): "
read operator
echo "Enter the second operand: "
read value2
if [ " $ operator " = " + " ] ; then
answer=$(echo "scale=2;value1+value2" |bc);
elif [ " $ operator " = " - " ] ; then
answer=$(echo " scale=2;value1-value2" |bc);
elif [ " $ operator " = " / " ] ; then
answer=$(echo "scale=2;value1/value2" |bc);
elif [ " $ operator " = " * " ] ; then
answer=$(echo "scale=2;value1*value2" |bc);
elif [ " $ operator " = " % " ] ; then
answer=$(echo "scale=2;value1%value2" |bc);
fi
echo "Answer: $value1 $operator $value2 = $answer";
done
|
|
|
11-26-2007, 10:30 PM
|
#5
|
Senior Member
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113
Rep: 
|
Oh, and on the other one, are you sure you have any zero-byte files to check? +0 should return all non-empty files, which is the opposite of what you want. Could try '-type f -empty'.
|
|
|
11-27-2007, 11:01 AM
|
#6
|
LQ Newbie
Registered: Nov 2007
Location: OK
Distribution: Fedora 4 -I think
Posts: 7
Original Poster
Rep:
|
Thank you for your suggestions. I realize the format is odd, I've seen better examples of doing a calculator, but the instructor is requesting that you ask the user in that manner and to enter "x" to exit, so I have to work with that. So I couldn't quite work out doing a case with the format he is wanting, which is why I used the "if" statements. Goodness gracious I'm tired of instructors giving us unconventional formats. Anyway, I'm sorry my code is sloppy, I have redone it fifty billion times and was in a hurry to get it posted and didn't think to clean it up first. Also, on the test with the "$" I'm not sure what to put for a test. Is there a symbol, character, or variable that would mean "input"? All the ones I've seen have used "$#", which I'm assuming means a number. And thank you for clarifying my other question about the empty files, I haven't tested it yet, but that makes more sense.
|
|
|
11-27-2007, 01:39 PM
|
#7
|
Senior Member
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113
Rep: 
|
Yeah, I think the simplest 'shell' calculator/bc wrapper for basic operations (which I can post since I know the format isn't acceptable for your problem) is
Code:
:declare -f calc
calc ()
{
echo "scale=5; $@" | bc -l
}
As far as $ARG, yes, $# is the number of arguments. For the contents of arguments, there's $* and $@ (and $1..., of course). Your shell manual should have details. But if you have to loop, one way to make a minimal change to the script is to just
As far as getting the case to work, you'd need to escape the multiplication symbol and should drop the scale/library for the modulus. But that's actually not important - either one works.
Other thoughts - I assume this is to be portable, so the 'echo' and 'read's are as they should be, but bash (at least) has a 'read -p VAR' that can echo and read at once. And you do no error checking for garbage input and don't clear the answer variable at the end of the loop, so you can get '3*3=9; 3 banana bingo=9'. (Which is kinda cool, actually, but probably not what your instructor wants.  )
|
|
|
09-28-2009, 11:46 PM
|
#8
|
LQ Newbie
Registered: Sep 2009
Posts: 2
Rep:
|
If all you need is a dirt simple calculator and you're using bash (which you probably are),
function calc() { echo $[$*]; }
from that site will do what you need.
|
|
|
09-28-2009, 11:47 PM
|
#9
|
LQ Newbie
Registered: Sep 2009
Posts: 2
Rep:
|
|
|
|
09-29-2009, 12:49 AM
|
#10
|
Member
Registered: Nov 2008
Location: ~/
Distribution: Arch || Sidux
Posts: 393
Rep:
|
Quote:
Originally Posted by BecauseRobots
If all you need is a dirt simple calculator and you're using bash (which you probably are),
function calc() { echo $[$*]; }
from that site will do what you need.
|
Brilliant... I am impressed 
|
|
|
09-29-2009, 06:15 AM
|
#11
|
Senior Member
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113
Rep: 
|
Bash doesn't do floating point, though - you'd need ksh or zsh (or bc) for that. I don't know the best ksh syntax but, at a couple extra chars, 'function kalc { echo $(($*)); }' would do.
|
|
|
09-29-2009, 04:43 PM
|
#12
|
Member
Registered: Sep 2009
Posts: 110
Rep:
|
This will fail: while [$ != 'x' ]; do
This will work: while [ $ != 'x' ]; do
(BASH is picky about whitespaces!)
This will fail: if [ " $ operator " = " + " ] ; then
This will work: if [ " $operator " = " + " ] ; then
This will fail: answer=$(echo "scale=2;value1+value2" | bc);
This will work: answer=$(echo "scale=2;$value1+$value2" | bc);
(Do the same for the other if conditions!)
Here is my version of your calculator:
Code:
#!/bin/sh
while [ $ != 'x' ]; do
echo "Welcome to calculator (x to quit)"
echo "Enter the first operand: "
read value1
if [[ $value1 == x ]]; then exit; fi
echo "Enter an operator (+, -, *, /): "
read operator
echo "Enter the second operand: "
read value2
if [ " $operator " = " + " ] ; then
answer=$(echo "scale=2;$value1+$value2" | bc);
elif [ " $ operator " = " - " ] ; then
answer=$(echo " scale=2;$value1-$value2" |bc);
elif [ " $ operator " = " / " ] ; then
answer=$(echo "scale=2;$value1/$value2" |bc);
elif [ " $ operator " = " * " ] ; then
answer=$(echo "scale=2;$value1*$value2" |bc);
elif [ " $ operator " = " % " ] ; then
answer=$(echo "scale=2;$value1%$value2" |bc);
fi
echo "Answer: $value1 $operator $value2 = $answer";
done
Last edited by AngTheo789; 09-29-2009 at 04:45 PM.
Reason: code tag and typo
|
|
|
11-06-2009, 12:44 AM
|
#13
|
LQ Newbie
Registered: Nov 2009
Posts: 2
Rep:
|
Hi,
Cant we try simple calculator script as below:
echo "enter first number"
read a
echo "enter second number"
read b
echo "enter operand"
read c
calc=`expr $a "$c" $b`
echo the result is $calc
|
|
|
All times are GMT -5. The time now is 12:58 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|