LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 05-04-2009, 12:24 PM   #1
JamesDoom
LQ Newbie
 
Registered: May 2009
Posts: 4

Rep: Reputation: 0
Simple Script Troubles (Please help)


I've programmed in C++ for a while and recently decided to try to learn a bit of Linux and am just trying to do a simple little calculator and I'm having a bunch of trouble.

I'm skipping most of the stupid code that isn't necessary aka most of the echos and user prompts

but:
The user is prompted to enter two numbers NUM1 and NUM2, after which they have another prompt to select which operator to use.

Code:
echo -n "please enter the first number"
read NUM1

echo -n "Please enter the second number."
read NUM2

echo -n "Please enter which operator you would like to use"
read selection

case $selection in
m) let ans=NUM1*NUM2; echo $ans;;
*) echo -n "Please enter proper data"
esac

echo " "
the output gives me a '0' every time..why is that?


edit: this is only one of the cases because that's as far as I've gotten so far, it will be doing more.
 
Old 05-04-2009, 12:51 PM   #2
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
I'm not having any issue with your code. Perhaps something you didn't include is causing your problem.

Forrest

p.s. A cleaner way to prompt for variables is to use the -p option to read. Like this:

read -p "Please enter the first number: " NUM1

Last edited by forrestt; 05-04-2009 at 12:53 PM.
 
Old 05-04-2009, 01:03 PM   #3
JamesDoom
LQ Newbie
 
Registered: May 2009
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by forrestt View Post
I'm not having any issue with your code. Perhaps something you didn't include is causing your problem.

Forrest

p.s. A cleaner way to prompt for variables is to use the -p option to read. Like this:

read -p "Please enter the first number: " NUM1
I'll post all the code in one second.

edit soon


Code:
echo -n "Which simple math equation would you like to use?"

echo  "Type the letter 'a' if you want addition."

echo -n "Type the letter 's' if you want subtraction."

echo -n "Type the letter 'd' if you want division."


echo -n "type the letter 'm' if you want multiplication."
echo -n ""

echo -n "please enter the first number: "
read NUM1

echo -n "Please enter the second number: "
read NUM2
echo " "
echo -n "Now please enter the selection from the top: "
read selection
case $selection in
m) let ANS = $NUM1*$NUM2; echo $ANS;;
d) let ANS=NUM1/NUM2 ; echo $ANS;;
a)let ANS=NUM1+NUM2 ; echo $ANS;;
*) echo "Proper data is required";;
esac

echo " "
error message:
./TestingScript: line 22: let: =: syntax error: operand expected (error token is "=")

Last edited by JamesDoom; 05-04-2009 at 01:05 PM.
 
Old 05-04-2009, 01:21 PM   #4
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
OK, I had to change

Code:
m) let ANS = $NUM1*$NUM2; echo $ANS;;
to

Code:
m) let ANS=$NUM1*$NUM2; echo $ANS;;
But everything else worked fine.

Here is how I would write this for a more readable script:

Code:
#!/bin/bash

echo "Which simple math equation would you like to use?"
echo "          'a' if you want addition."
echo "          's' if you want subtraction."
echo "          'd' if you want division."
echo "          'm' if you want multiplication."

read -p "please enter the first number: " NUM1

read -p "Please enter the second number: " NUM2

read -p "Now please enter the selection from the top: " selection

case $selection in
        m)
                let ANS=$NUM1*$NUM2
                ;;
        d)
                let ANS=NUM1/NUM2
                ;;
        a)
                let ANS=NUM1+NUM2
                ;;
        *)
                echo "Proper data is required"
                ;;
esac
echo $ANS

Last edited by forrestt; 05-04-2009 at 01:25 PM.
 
Old 05-04-2009, 01:23 PM   #5
JamesDoom
LQ Newbie
 
Registered: May 2009
Posts: 4

Original Poster
Rep: Reputation: 0
I'll log back on linux and give it a try, I don't know why but no one else would respond on any other forums that I normally go to.

be right back.
 
Old 05-04-2009, 01:41 PM   #6
JamesDoom
LQ Newbie
 
Registered: May 2009
Posts: 4

Original Poster
Rep: Reputation: 0
I got it to work all thanks to you.

Thank you so much for your help.
 
Old 05-05-2009, 02:28 AM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,360

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
You may find this useful if you don't already have it: http://www.tldp.org/LDP/abs/html/
 
  


Reply



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
Need help getting started simple simple shell script dhonnoll78 Programming 6 12-17-2007 05:34 PM
Iptables (with masq) troubleshooting, very simple script attached script and logs. xinu Linux - Networking 13 11-01-2007 04:19 AM
Cron script troubles Galphar Linux - Newbie 1 01-19-2005 01:32 PM
some troubles with a script Ciccio Linux - Software 3 03-22-2003 07:53 AM
Simple C Shell script is not so simple elconde Programming 2 09-16-2001 11:53 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:28 PM.

Main Menu
Advertisement
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
Twitter: @linuxquestions
Open Source Consulting | Domain Registration