LinuxQuestions.org
Review your favorite Linux distribution.
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 12-22-2008, 09:58 AM   #1
mmahulo
Member
 
Registered: Nov 2008
Posts: 31

Rep: Reputation: 15
Simple arithmetic using expr


Hi

I am trying to use expr to do simple arithmetic. I read user input (like 2+3) from the keyboard and store it in a variable. When I use expr to perform the calculation it needs me to make a space between the numbers and the operand. Another thing is that when I multiply it gives me an error saying "expr: Syntax error".
Here is my code:

while true
do
echo "Enter Arithmetic Calculation:"
read calc
echo `expr $calc`
done




Please help as usual. Thanks in advance!
 
Old 12-22-2008, 10:23 AM   #2
hasanatizaz
Member
 
Registered: Nov 2007
Location: Pakistan
Distribution: Redhat and Debian
Posts: 302
Blog Entries: 1

Rep: Reputation: 34
i have never used expr... another way of doing is
read calc
echo $((calc))

Last edited by hasanatizaz; 12-22-2008 at 10:24 AM.
 
Old 12-22-2008, 10:26 AM   #3
hasanatizaz
Member
 
Registered: Nov 2007
Location: Pakistan
Distribution: Redhat and Debian
Posts: 302
Blog Entries: 1

Rep: Reputation: 34
oh see if this works
read calc
echo `expr $((calc))`
 
Old 12-22-2008, 10:27 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
I offer two alternatives: 1) you can pass the calculation to bc without modifying the input, i.e.
Code:
echo $calc | bc
2) in bash you can try the arithmetic expansion, i.e.
Code:
echo $(($calc))
If you really want to use expr, take in mind that the multiplication operator must be escaped as \*. So you have to parse the input and transform the arithmetic operators to escape them and/or to add blank spaces, but this is a little more tricky and if you really don't need it, I suggest one of the two methods mentioned above.
 
Old 12-22-2008, 10:33 AM   #5
GazL
Senior Member
 
Registered: May 2008
Posts: 4,091
Blog Entries: 1

Rep: Reputation: 1541Reputation: 1541Reputation: 1541Reputation: 1541Reputation: 1541Reputation: 1541Reputation: 1541Reputation: 1541Reputation: 1541Reputation: 1541Reputation: 1541
expr is a little choosy about its parameter, you must space them correctly, and in the case of a * you have to escape it to stop the shell from treating it as a wildcard, which is why you get the syntax error.

If you're using the bash shell try this instead of the expr line.

Code:
echo $[$calc]
edit:

I see others have recommended $(( )) rather than $[ ] and as $(( )) appears to be listed in the manual pages for arithmetic you're probably better off sticking with that. Anyone know where I've picked up $[ ] from? Is it old bourne shell syntax or something?

Last edited by GazL; 12-22-2008 at 10:43 AM.
 
Old 12-22-2008, 11:24 AM   #6
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 727

Rep: Reputation: 74
Hi.
Quote:
The old format $[expression] is deprecated and will be removed in
upcoming versions of bash.

-- excerpt from man bash 2.05b.0
cheers, makyo
 
Old 12-22-2008, 11:26 AM   #7
GazL
Senior Member
 
Registered: May 2008
Posts: 4,091
Blog Entries: 1

Rep: Reputation: 1541Reputation: 1541Reputation: 1541Reputation: 1541Reputation: 1541Reputation: 1541Reputation: 1541Reputation: 1541Reputation: 1541Reputation: 1541Reputation: 1541
Ahh, its an old bashism is it. I guess I'm showing my age again.
Thanks makyo.
 
Old 12-22-2008, 11:27 AM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Also an excerpt from the Advanced Bash Scripting Guide, Example 8.2 Using Arithmetic Operations
Code:
n=$(($n + 1))
echo -n "$n "

: $[ n = $n + 1 ]
#  ":" necessary because otherwise Bash attempts
#+ to interpret "$[ n = $n + 1 ]" as a command.
#  Works even if "n" was initialized as a string.
echo -n "$n "

n=$[ $n + 1 ]
#  Works even if "n" was initialized as a string.
#* Avoid this type of construct, since it is obsolete and nonportable.
#  Thanks, Stephane Chazelas.
echo -n "$n "
 
Old 12-23-2008, 02:48 AM   #9
mmahulo
Member
 
Registered: Nov 2008
Posts: 31

Original Poster
Rep: Reputation: 15
Thanks guys!

I used the line echo `expr $((calc))` and it worked perfectly fine that I didn't even had to bother escaping the * character. Thank you all!!

Last edited by mmahulo; 12-23-2008 at 02:49 AM.
 
  


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
How to multiply with expr? ( expr 3 * 4 doesn't work, though expr 3 + 4 does) lumix Linux - General 5 02-25-2009 06:31 PM
LXer: Simple Arithmetic In Bash, Perl and Awk - More Porting LXer Syndicated Linux News 0 05-28-2008 09:41 AM
BASH, simple arithmetic helptonewbie Programming 9 01-10-2007 06:53 PM
simple arithmetic in bash gfrair Linux - Newbie 9 03-16-2005 03:09 PM
simple expr izza_azhar Programming 3 01-17-2005 08:38 PM


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