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 > Shell Programming and Scripting
.
google unix.com



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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Help with arithmetic operation emjayshaikh Shell Programming and Scripting 3 09-23-2007 11:44 PM
Can I use wc -l with arithmetic expression? lalelle Shell Programming and Scripting 3 08-11-2007 06:44 PM
Arithmetic In UNIX tygun UNIX for Dummies Questions & Answers 3 11-23-2005 05:46 PM
arithmetic syntax paprbagprincess UNIX for Dummies Questions & Answers 1 05-11-2004 08:30 AM
time arithmetic csong2 UNIX for Advanced & Expert Users 2 11-28-2001 11:57 AM

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 01-30-2007
amon amon is offline
Registered User
  
 

Join Date: Jan 2006
Posts: 71
arithmetic in ksh

Helloo..

I am trying one very simple thing I could not find anything on google..


I have 2 integer variable..and I need to do division...in ksh
where $catch and $num are integer variable..

I tryed with this:

printf "%0.2f" $final=$catch/$num

but it does not work..

any help is welcome..
  #2 (permalink)  
Old 01-30-2007
andryk's Avatar
andryk andryk is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2003
Posts: 448
try this one
Code:
export final=`expr $catch / $num`
echo $final
You might also use bc ...
  #3 (permalink)  
Old 01-30-2007
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
Code:
$x=4
$y=2
$expr $x / $y
2
$echo $(( x / y ))
2
$echo $x / $y | bc
2

Last edited by anbu23; 01-30-2007 at 08:06 AM..
  #4 (permalink)  
Old 01-30-2007
amon amon is offline
Registered User
  
 

Join Date: Jan 2006
Posts: 71
Rightttt but this is not wanted result..

I tryed also bc I forgot to mention that..

but my $catch is 14 $num is 100

it is defined above in program..


result should be 0,14 also I tryed this:

x=`echo $catch / $num | bc`
echo $x

but this does not give goot results..
  #5 (permalink)  
Old 01-30-2007
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
Code:
$echo "scale=2; 14 / 100" | bc
.14
  #6 (permalink)  
Old 01-30-2007
amon amon is offline
Registered User
  
 

Join Date: Jan 2006
Posts: 71
well..looks lke that is it..

thanks for quick info..
  #7 (permalink)  
Old 01-30-2007
d.f. d.f. is offline
Registered User
  
 

Join Date: Nov 2006
Posts: 5
Quote:
Originally Posted by anbu23
Code:
$x=4
[...snip...]

$echo $(( x / y ))
2

[...snip...]
Isn't this the "preferred" way of doing arithmetic in ksh and bash? I recall reading that it doesn't spawn a new shell like expr and bc.
Sponsored Links
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 04:18 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