The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



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 !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Floating Division in Linux Dastard Shell Programming and Scripting 3 04-16-2008 12:57 PM
Round with division command vanloonmichel Shell Programming and Scripting 13 02-15-2007 09:32 AM
division in shell scripts esham Shell Programming and Scripting 6 11-30-2005 06:42 AM
Cannot adjust division jav_v Filesystems, Disks and Memory 1 08-20-2002 08:50 AM
division problem inquirer Shell Programming and Scripting 3 04-02-2002 10:03 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-10-2006
rochitsharma rochitsharma is offline
Registered User
  
 

Join Date: Feb 2006
Posts: 82
Problem in division

hi
I am having two variables namely a=7 & b=8. I have to subtract these two variables. I am using the command

c=`expr $a / $b`

When I check the value of c, it comes out to be zero.
Please help.
Regards
Rochit
  #2 (permalink)  
Old 08-10-2006
tmarikle tmarikle is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2005
Posts: 683
According to the MAN page:
Code:
     expr { \*, /, %} expr
           Multiplication,  division,   or   remainder   of   the
           integer-valued arguments.
what would you expect? Your answer will be zero. To get the remainder use the percent sign.

You can use awk:
Code:
 nawk 'BEGIN {print 7/8}'
  #3 (permalink)  
Old 08-10-2006
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
Code:
quoting 'man expr':
     expr1 {*, /, %} expr2
             Return the results of multiplication, integer division, or
             remainder of integer-valued arguments.
Code:
a=7
b=8
echo "scale=2; $a / $b" | bc
  #4 (permalink)  
Old 08-10-2006
rochitsharma rochitsharma is offline
Registered User
  
 

Join Date: Feb 2006
Posts: 82
Dear tmarikle & vgersh99,
Thanks alot both of you for helping. both solutions are working
But it will be very nice of you vgesh99 if u could explain me the command
bc

Regards
Rochit
  #5 (permalink)  
Old 08-10-2006
tmarikle tmarikle is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2005
Posts: 683
Code:
nawk -v val_a=$a -v val_b=$b 'BEGIN {print val_a / val_b}'
or

Code:
nawk 'BEGIN {print '$a' / '$b'}'
  #6 (permalink)  
Old 08-10-2006
rochitsharma rochitsharma is offline
Registered User
  
 

Join Date: Feb 2006
Posts: 82
hi tmarkle
could u explain me your code

nawk -v val_a=$a -v val_b=$b 'BEGIN {print val_a / val_b}'

Thanks alot
Regards
Rochit
  #7 (permalink)  
Old 08-10-2006
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
Quote:
Originally Posted by rochitsharma
Dear tmarikle & vgersh99,
Thanks alot both of you for helping. both solutions are working
But it will be very nice of you vgesh99 if u could explain me the command
bc

Regards
Rochit
take a quick look at 'man bc' - it should be self-explanatory.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:42 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0