LinuxQuestions.org
Visit Jeremy's Blog.
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 10-24-2013, 09:04 AM   #1
Tauatioti
LQ Newbie
 
Registered: Oct 2013
Posts: 18

Rep: Reputation: Disabled
How to use if statement with "expr"


Hi everyone

Im trying to write a simple script the can calculate the price of something, but when i run the script there is an error "expr: syntax error". here is my script

#ex1

shirt=\$15.00
black=\$13.50


echo "Please enter how many shirt you want"
read num1
echo
echo "Please choose"
echo "1 ---> Normal Shirt"
echo "2 ---> black Shirt"
read choice
echo


if [ $choice = "1" ]; then
answer=`expr $num1 * $shirt`
echo "$num1 * $shirt = $answer"
elif [ $choice = "2" ]; then
answer=`expr $num1 * $black`
echo "$num1 - $num2 = $answer"

else
echo "invalid selection"

fi

Please anyone can help tell me what was missing in the script?

sory for my poor english

Thanks

Tauatioti

Last edited by Tauatioti; 10-24-2013 at 09:07 AM.
 
Old 10-24-2013, 09:25 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
You are running into 2 problems:

1 - The * is special when using `expr $num1 * $shirt`. This causes the expr: syntax error
You need to escape it: `expr $num1 \* $shirt`

2 - This is the biggest problem: Bash doesn't handle fractions that well. This may cause the following error:
Code:
num=3
shirt=13.5
total=`expr $num1 \* $shirt`
expr: non-integer argument
Using bc can solve all the above:
Code:
num=3
shirt=13.5
total=$( echo "$shirt * $amount" | bc )
BTW: Don't do this:
Code:
shirt=\$15.00
black=\$13.50
The dollar sign can be added to the amount/price individually. If you don't you need to remove the $ from the string before you do the arithmetic.
 
1 members found this post helpful.
Old 10-24-2013, 10:50 AM   #3
Tauatioti
LQ Newbie
 
Registered: Oct 2013
Posts: 18

Original Poster
Rep: Reputation: Disabled
Hello druuna

Thanks for your quick reply, my first problem is solved, and i try your step to solve the second one (fraction) but it not working, any suggestion?

Here is my script

#exercise 3

clear
shirt=15
black=13

echo "The price of the shirt is listed below"
echo "Normal shirt \$15"
echo "Black shirt \$13.5"


echo "Please enter how many shirt you want"
read num1
echo
echo "Please choose"
echo "1 ---> Normal Shirt"
echo "2 ---> black Shirt"
read choice
echo


if [ $choice == "1" ]; then
answer=`expr $num1 \* $shirt`
echo "$num1 * $shirt = $answer"
echo "The Price is $answer dollars for $num1 Shirt"
elif [ $choice = "2" ]; then
total=$( echo "$black * $num1" | bc )


else
echo "invalid selection"

fi
 
Old 10-24-2013, 11:05 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
There are multiple errors/typo's in your code.

Have a look at this:
Code:
if [ $choice == "1" ]
then
  answer=$( echo "$num1 * $shirt" | bc )  # just in case, if the price goes up (15.00 -> 15.25) it will still work.
  echo "$num1 * $shirt = $answer"
  echo "The Price is $answer dollars for $num1 Shirt"
elif [ $choice == "2" ]   # == instead of =
then
  total=$( echo "$black * $num1" | bc )
  echo "The Price is $total dollars for $num1 Shirts"   # you never print the answer....
else
  echo "invalid selection"
fi
 
1 members found this post helpful.
Old 10-24-2013, 11:37 AM   #5
Tauatioti
LQ Newbie
 
Registered: Oct 2013
Posts: 18

Original Poster
Rep: Reputation: Disabled
you are right about my script.... i know that i solved my first problem using \* instead of *... but could you help on how i calculate decimal numbers

Thanks
 
Old 10-24-2013, 11:43 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Have a look at these links:

- Perform arithmetic operations
- Chapter 13. Arithmetic Expansion
- Arithmetic Expressions in BASH
 
1 members found this post helpful.
Old 10-24-2013, 11:55 AM   #7
Tauatioti
LQ Newbie
 
Registered: Oct 2013
Posts: 18

Original Poster
Rep: Reputation: Disabled
I'll look at now, anyway thank you for your time

God bless 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
"Command not found" error using expr in shell script Harish sharma Linux - Newbie 17 11-08-2012 06:53 AM
swatch: Multiple fields in "threshold track_by" statement kenneho Linux - Server 0 06-09-2011 06:19 AM
[SOLVED] A confusing statement about "locking an account" in the ClamAV documentation. pr_deltoid Linux - Software 3 06-24-2010 05:09 AM
XLF compile HELP !The unformatted I/O statement on the file "..." cannot be completed chonchis AIX 0 06-12-2009 02:22 PM
FC6 firefox hangs with "expr: syntax error" srn1 Fedora 0 08-16-2007 10:45 PM


All times are GMT -5. The time now is 04:44 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration