LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux > Linux - Newbie
User Name
Password
Linux - Newbie This 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

Reply
 
Thread Tools
Old 11-26-2007, 08:54 PM   #1
chellemybelle
LQ Newbie
 
Registered: Nov 2007
Location: OK
Distribution: Fedora 4 -I think
Posts: 7
Thanked: 0
Question Simple Shell Script Calculator


[Log in to get rid of this advertisement]
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.
chellemybelle is offline     Reply With Quote
Old 11-26-2007, 09:16 PM   #2
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware 13.0, -current
Posts: 3,887
Blog Entries: 1
Thanked: 27
If this is homework like your other question was, we really shouldn't be helping you out with it.
slakmagik is offline     Reply With Quote


Old 11-26-2007, 09:51 PM   #3
chellemybelle
LQ Newbie
 
Registered: Nov 2007
Location: OK
Distribution: Fedora 4 -I think
Posts: 7
Thanked: 0

Original Poster
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.
chellemybelle is offline     Reply With Quote


Old 11-26-2007, 10:27 PM   #4
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware 13.0, -current
Posts: 3,887
Blog Entries: 1
Thanked: 27
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
slakmagik is offline     Reply With Quote


Old 11-26-2007, 10:30 PM   #5
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware 13.0, -current
Posts: 3,887
Blog Entries: 1
Thanked: 27
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'.
slakmagik is offline     Reply With Quote


Old 11-27-2007, 11:01 AM   #6
chellemybelle
LQ Newbie
 
Registered: Nov 2007
Location: OK
Distribution: Fedora 4 -I think
Posts: 7
Thanked: 0

Original Poster
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.
chellemybelle is offline     Reply With Quote


Old 11-27-2007, 01:39 PM   #7
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware 13.0, -current
Posts: 3,887
Blog Entries: 1
Thanked: 27
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
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. )
slakmagik is offline     Reply With Quote


Old 09-28-2009, 11:46 PM   #8
BecauseRobots
LQ Newbie
 
Registered: Sep 2009
Posts: 2
Thanked: 0
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.
windows_vista BecauseRobots is offline     Reply With Quote


Old 09-28-2009, 11:47 PM   #9
BecauseRobots
LQ Newbie
 
Registered: Sep 2009
Posts: 2
Thanked: 0
Forgot to mention that's from http://becauserobots.com/2009/09/22/...ll-calculator/
windows_vista BecauseRobots is offline     Reply With Quote


Old 09-29-2009, 12:49 AM   #10
manwithaplan
Member
 
Registered: Nov 2008
Location: ~/
Distribution: Arch || Sidux
Posts: 393
Thanked: 29
Quote:
Originally Posted by BecauseRobots View Post
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
linuxgentoo manwithaplan is offline     Reply With Quote


Old 09-29-2009, 06:15 AM   #11
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware 13.0, -current
Posts: 3,887
Blog Entries: 1
Thanked: 27
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.
linuxslackware slakmagik is offline     Reply With Quote


Old 09-29-2009, 04:43 PM   #12
AngTheo789
Member
 
Registered: Sep 2009
Posts: 60
Thanked: 14
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
linuxfedora AngTheo789 is offline     Reply With Quote


Old 11-06-2009, 12:44 AM   #13
anirudhm
LQ Newbie
 
Registered: Nov 2009
Posts: 2
Blog Entries: 1
Thanked: 0
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
windows_xp_2003 anirudhm is offline     Reply With Quote



Reply

Bookmarks


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
simple shell script sycamorex Linux - Newbie 2 03-16-2006 07:00 PM
Simple Shell Script Help Kristijan Programming 3 06-13-2005 10:13 PM
simple shell script Greg_courageous Programming 2 05-12-2004 05:34 PM
a simple shell script Warchief Programming 1 07-31-2003 06:01 AM
Simple C Shell script is not so simple elconde Programming 2 09-17-2001 12:53 AM


All times are GMT -5. The time now is 12:21 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration