how do I get the value of expr with ksh | Unix Linux Forums | UNIX for Advanced & Expert Users

  Go Back    


UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

how do I get the value of expr with ksh

UNIX for Advanced & Expert Users


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 08-24-2001
krishna krishna is offline
Registered User
 
Join Date: Aug 2001
Last Activity: 3 August 2007, 4:25 AM EDT
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
how do I get the value of expr with ksh

Hi,

I have written a korn shell script to compute the value of k.

formulae :

a=10
b=20
c=30
k=(a+b)*c

my shell script is :

a=10
b=20
c=30
k=`expr (($a + $b ) * $c )`
echo $k

### here paranthesis ( ) not accepting by expr function.
### if i remove paranthesis, the value will be 610(wrong)
### but the actual value should be 900

please give me solution to kkodava@maxis.com.my

thanks

krishna
Sponsored Links
    #2  
Old 08-24-2001
Michael Michael is offline
Registered User
 
Join Date: May 2001
Last Activity: 8 November 2001, 8:00 AM EST
Location: Germany
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Hi Krisna,

try it with "let"

example:

let k="($a + $b) * $c"

k=900

greetings
Sponsored Links
    #3  
Old 08-25-2001
patvdv's Avatar
patvdv patvdv is offline
Registered User
 
Join Date: Jul 2001
Last Activity: 14 March 2003, 5:08 AM EST
Location: Belgium
Posts: 83
Thanks: 0
Thanked 0 Times in 0 Posts
KSH Maths

Why using 'expr' if you can use the built-in KSH math functions instead:

a=10
b=20
c=30
k=$ ((($a+$b)*$c )))
echo $k

    #4  
Old 08-26-2001
rwb1959's Avatar
rwb1959 rwb1959 is offline
Registered User
 
Join Date: Aug 2001
Last Activity: 16 October 2009, 2:04 PM EDT
Location: Virginia, USA
Posts: 438
Thanks: 0
Thanked 1 Time in 1 Post
using expr

"expr" evaluates 2 and only 2 expressions.
In order to accomplish what you want to do
using "expr" you need to nest expr's and
of course, remember to "escape" the speecial
characters. An example would be...

echo `expr \`expr 1 + 2\` \* 3`

This expression, when run at the command
prompt, should yield 9. Of course, if you
are using ksh and not planning on running
a different shell, I would stick with the
ksh built-in capabilities.
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
Error with expr - "expr: syntax error" jagari Shell Programming and Scripting 6 02-24-2012 05:25 AM
expr ?! Mad van Bert UNIX for Dummies Questions & Answers 1 09-06-2011 03:28 AM
the expr \* jackel7777 Shell Programming and Scripting 1 07-29-2011 10:29 PM
test expr VS [ expr ] ashok.g Shell Programming and Scripting 2 12-03-2009 06:42 AM
how to use expr saikumarm80 Red Hat 3 01-05-2009 02:52 PM



All times are GMT -4. The time now is 10:26 AM.