how to use the expr expression in UNIX? | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

how to use the expr expression in UNIX?

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-03-2009
psiva_arul's Avatar
psiva_arul psiva_arul is offline
Registered User
 
Join Date: Jul 2007
Last Activity: 19 November 2009, 6:58 AM EST
Location: Bangalore, India
Posts: 97
Thanks: 0
Thanked 1 Time in 1 Post
how to use the expr expression in UNIX?

Hi all,

I'm facing a problem with "expr" command while calculating value inside the shell script (c shell). if i passed direct number to expr command its working but if passed value through variable its not working.

For example:

set Result=`expr 500 + 50`
echo $Result
output : 550

But if i pass value through variable its not working
$A=150.15
$B=25.20

set Results=`expr $A + $B`
echo $Results
output = 0

Could you please anyone help me on this?

Thanks
MPS
Sponsored Links
    #2  
Old 03-03-2009
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 22 September 2014, 4:44 PM EDT
Location: NM
Posts: 10,211
Thanks: 278
Thanked 796 Times in 743 Posts
expr only accepts whole number arguments - integers.

That is why you got zero. It will work with environment variables.
Sponsored Links
    #3  
Old 03-03-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
 
Join Date: Mar 2007
Last Activity: 31 August 2014, 7:32 PM EDT
Location: Toronto, Canada
Posts: 2,877
Thanks: 0
Thanked 110 Times in 102 Posts
Quote:
Originally Posted by psiva_arul View Post
I'm facing a problem with "expr" command while calculating value inside the shell script (c shell).

Top Ten Reasons not to use the C shell
Csh problems
Csh Programming Considered Harmful
Quote:
if i passed direct number to expr command its working but if passed value through variable its not working.

For example:

set Result=`expr 500 + 50`
echo $Result
output : 550

But if i pass value through variable its not working
$A=150.15
$B=25.20

set Results=`expr $A + $B`
echo $Results
output = 0

expr only does integer arithmetic.
    #4  
Old 03-04-2009
psiva_arul's Avatar
psiva_arul psiva_arul is offline
Registered User
 
Join Date: Jul 2007
Last Activity: 19 November 2009, 6:58 AM EST
Location: Bangalore, India
Posts: 97
Thanks: 0
Thanked 1 Time in 1 Post
How can we calculate the floating data with expr command?

Thanks for your reply,

How can we calculate the floating data with expr command?
Eg:
$A=150.15
$B=25.20
set Results=`expr $A + $B`
echo $Results

Could you please send me the actual solution for this?


Thanks
MPS
Sponsored Links
    #5  
Old 03-04-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
 
Join Date: Mar 2007
Last Activity: 31 August 2014, 7:32 PM EDT
Location: Toronto, Canada
Posts: 2,877
Thanks: 0
Thanked 110 Times in 102 Posts
Quote:
Originally Posted by psiva_arul View Post
Thanks for your reply,

How can we calculate the floating data with expr command?

You can't; expr only does integer arithmetic.
Quote:
Eg:
$A=150.15
$B=25.20
set Results=`expr $A + $B`
echo $Results

Could you please send me the actual solution for this?

Use awk or bc.

I use this function for floating-point arithmetic:


Code:
 calc()
  {
  awk 'BEGIN { OFMT="%f"; print '"$*"'; exit}'
  }

And call it with, for example:


Code:
calc '3.2 + 4.3'

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to use expr saikumarm80 Red Hat 3 01-05-2009 02:52 PM
error: initializer expression list treated as compound expression arunchaudhary19 Programming 12 11-16-2007 05:44 AM
expr Nathe UNIX for Dummies Questions & Answers 5 01-22-2004 06:49 PM
Regular Expression + Aritmetical Expression Z0mby Shell Programming and Scripting 2 05-21-2002 10:59 AM
compound expression in unix spalmer UNIX for Dummies Questions & Answers 3 10-19-2001 12:04 PM



All times are GMT -4. The time now is 04:20 AM.