Multiple arithmetic operations in a single line | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Multiple arithmetic operations in a single line

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-15-2013
midhun19 midhun19 is offline
Registered User
 
Join Date: Nov 2009
Last Activity: 17 September 2013, 6:54 AM EDT
Posts: 36
Thanks: 22
Thanked 0 Times in 0 Posts
Multiple arithmetic operations in a single line

hi,

I want to do multiple arithmetic operations in a single line. I tried with the following line


Code:
total_size=`expr ($total_size + $file_size) / 1024`

this line is throwing me error.

Im working on korn shell. Can i know how to do multiple arithmetic operations in a single line
Sponsored Links
    #2  
Old 03-15-2013
Don Cragun's Avatar
Don Cragun Don Cragun is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 23 July 2014, 10:05 PM EDT
Location: San Jose, CA, USA
Posts: 4,124
Thanks: 160
Thanked 1,410 Times in 1,195 Posts
With a recent Korn shell (newer than 1988):

Code:
total_size=$(( (total_size + file_size) / 1024 ))

if you want integer arithmetic or:

Code:
total_size=$(( (total_size + file_size) / 1024. ))

if you want floating point arithmetic.
The Following User Says Thank You to Don Cragun For This Useful Post:
midhun19 (03-15-2013)
Sponsored Links
    #3  
Old 03-15-2013
elixir_sinari's Avatar
elixir_sinari elixir_sinari is offline Forum Advisor  
Gotham Knight
 
Join Date: Mar 2012
Last Activity: 16 July 2014, 3:22 PM EDT
Location: India
Posts: 1,412
Thanks: 100
Thanked 495 Times in 472 Posts

Code:
expr \( $total_size + $file_size \) / 1024

Parentheses are special for the shell. So, you need to escape them.
Also note the whitespace introduced between the parenthesis (both opening and closing) and the following/preceding token.
And beware, expr can only do integer arithmetic.

Hence, I would recommend using the arithmetic capabilities of the shell ( $((...)) ) itself (if you have them, that is) instead of an external expr command.

Last edited by elixir_sinari; 03-15-2013 at 11:23 PM..
The Following User Says Thank You to elixir_sinari For This Useful Post:
midhun19 (03-15-2013)
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
Multiple lines in a single column to be merged as a single line for a record Bhuvaneswari Shell Programming and Scripting 1 08-11-2011 03:16 AM
how to do arithmetic operations in one line command johnveslin Shell Programming and Scripting 2 07-01-2011 03:40 PM
arithmetic operations on 1 column of a file fadista UNIX for Dummies Questions & Answers 2 12-10-2010 12:26 PM
Arithmetic operations in bash,ksh,sh rprajendran Shell Programming and Scripting 1 02-01-2010 07:18 AM
problem in arithmetic operations ithirak17 Shell Programming and Scripting 3 02-06-2007 05:46 AM



All times are GMT -4. The time now is 12:13 AM.