LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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, 12: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, 12:53 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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 12:57 PM. Reason: Added link
 
Old 01-05-2013, 07: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



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

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

All times are GMT -5. The time now is 05:56 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