Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

how to use the expr expression in UNIX?

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 03-03-2009
psiva_arul's Unix or Linux Image
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 Unix and Linux 03-03-2009
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 25 May 2015, 10:29 PM EDT
Location: NM
Posts: 10,452
Thanks: 336
Thanked 860 Times in 799 Posts
expr only accepts whole number arguments - integers.

That is why you got zero. It will work with environment variables.
Sponsored Links
    #3  
Old Unix and Linux 03-03-2009
cfajohnson's Unix or Linux Image
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
 
Join Date: Mar 2007
Last Activity: 25 May 2015, 4:52 PM EDT
Location: Toronto, Canada
Posts: 2,883
Thanks: 0
Thanked 119 Times in 107 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 Unix and Linux 03-04-2009
psiva_arul's Unix or Linux Image
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 Unix and Linux 03-04-2009
cfajohnson's Unix or Linux Image
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
 
Join Date: Mar 2007
Last Activity: 25 May 2015, 4:52 PM EDT
Location: Toronto, Canada
Posts: 2,883
Thanks: 0
Thanked 119 Times in 107 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 Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 07:38 AM.