Linux - NewbieThis 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.
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.
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.
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.
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
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'.
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.
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
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
Code:
while :; do
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. )
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.
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 03:45 PM.
Reason: code tag and typo
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.