LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 01-05-2013, 01:08 PM   #1
charging-ibis
Member
 
Registered: Dec 2011
Location: Columbus, OH
Distribution: Slackware 13.37
Posts: 38

Rep: Reputation: 0
Angry Problem with shell script, laying in if/then/else & Case structure[SIMPLE MATH PROB.]


Hello linux users. I am studying for my Linux+ exam. I am on an exersice on doing a simple math problem w/ a desicion of calculating the sum or average of 3 numbers entered by the user. Everything is fine in the code until it steps through the case structure. I even had the same problems using the if/then/else. Here is my code:

Code:
#!/bin/bash
### The Name of the file is 'domath0' ###
clear
declare -i A
declare -i B
declare -i C
declare -i ANSWER
echo "Enter #1:"
read A
echo "Enter #2:"
read B
echo "Enter #3:"
read C
echo "Choose Process:"
echo "p : the sum"
echo "v : the average:"
echo "What is your decision?: "
read CHOICE
case $CHOICE in
	p | P ) ANSWER = A + B + C ##Here is line 19##
;;
	v | V ) ANSWER = A + B + C
	ANSWER = $ANSWER / 3
;;
esac
echo "The result is: " $ANSWER "." ## Here is line 25##
exit 0
Now here is the execution/output from shell:
Code:
Enter #1:
1
Enter #2:
2
Enter #3:
3
Choose Process:
p : the sum
v : the average:
What is your decision?: 
p
/home/cbattle4654/bin/domath0: line 19: ANSWER: command not found
The result is:  .
As a result also, output of line 25 is absent.
I would like to understand what is going on. Am I doing something that I am not seeing? Please clarify with me as soon as possible.
~Thanks
 
Old 01-05-2013, 01:53 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2394Reputation: 2394Reputation: 2394Reputation: 2394Reputation: 2394Reputation: 2394Reputation: 2394Reputation: 2394Reputation: 2394Reputation: 2394Reputation: 2394
You almost had it:
Code:
case $CHOICE in
    p | P ) (( ANSWER = A + B + C ))
;;
    v | V ) (( ANSWER = A + B + C ))
    (( ANSWER = ANSWER / 3 )) # $ also removed
This is arithmetic and you need to use double parentheses.

Bash Arithmetic Expression

Last edited by druuna; 01-05-2013 at 01:57 PM. Reason: Added link
 
Old 01-05-2013, 08:11 PM   #3
charging-ibis
Member
 
Registered: Dec 2011
Location: Columbus, OH
Distribution: Slackware 13.37
Posts: 38

Original Poster
Rep: Reputation: 0
Wink

Quote:
Originally Posted by druuna View Post
You almost had it:
Code:
case $CHOICE in
    p | P ) (( ANSWER = A + B + C ))
;;
    v | V ) (( ANSWER = A + B + C ))
    (( ANSWER = ANSWER / 3 )) # $ also removed
This is arithmetic and you need to use double parentheses.

Bash Arithmetic Expression
Oh my goodness thanks for your help I also read the simple arithmetic expressions that you referred me to and I can grow from this. I thank you.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Problem with case statement in shell script ayamkudy Programming 2 05-24-2011 03:18 PM
[SOLVED] script help (Case & IF statement syntax), easy to answer if you know shell scripting. casperpache Linux - Newbie 12 05-09-2011 08:18 AM
[SOLVED] Issues doing simple math in Bash script buee Linux - Newbie 10 07-20-2010 11:46 PM
How to do simple math in shell script gidrow Programming 5 07-30-2009 04:46 PM
C & kernel data structure & shell script vishalbutte Programming 7 01-13-2006 09:38 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

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

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration