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
Date Calculations mitschcg UNIX for Dummies Questions & Answers 8 03-06-2009 04:40 AM
replacing numbers greater than 0 with 1 vrms Shell Programming and Scripting 4 06-04-2008 08:40 AM
How to perform addition of two numbers in shell scripting in Solaris-10 krevathi1912 SUN Solaris 9 11-29-2007 09:36 AM
Time Calculations Nysif Steve UNIX for Dummies Questions & Answers 5 09-14-2007 09:49 AM
Float calculations sharmavr UNIX for Dummies Questions & Answers 1 07-26-2006 03:18 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-21-2005
stevefox stevefox is offline
Registered User
  
 

Join Date: Aug 2005
Posts: 115
Question How to perform calculations using numbers greater than 2150000000.

Could someone tell me how to perform calculations using numbers greater than 2150000000 in Korn Shell? When I tried to do it it gave me the wrong answer.

e.g. I have a ksh file with the contents below:
---------------------------------
#!/bin/ksh

SUM=`expr 2150000000 + 2`
PRODUCT=`expr 2150000000 "*" 2`
QUOTIENT=`expr 2150000000 / 2`

echo The sum is $SUM
echo The product is $PRODUCT
echo The quotient is $QUOTIENT
---------------------------------

It should give 2150000002 for SUM, 4300000000 for PRODUCT and 1075000000 for QUOTIENT but it outputs below:

The sum is -2144967294
The product is 5032704
The quotient is -1072483648

Last edited by stevefox; 11-21-2005 at 08:56 PM..
  #2 (permalink)  
Old 11-21-2005
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,122
PRODUCT=`echo '2150000000 * 2' | bc`
  #3 (permalink)  
Old 11-22-2005
stevefox stevefox is offline
Registered User
  
 

Join Date: Aug 2005
Posts: 115
Question

Thanks Perderabo!
I want to use that inside a For Loop but it does not work. Could someone tell me how to do this calculation inside a For Loop?

Below is the ksh containing the For Loop (the values of input will be greater than 2150000000):

#!/bin/ksh
for i in `cut -f1 -d" " input.txt | uniq`

do
dividend=0
divider=0
for j in `grep $i input.txt | cut -f4 -d" "`
do
(( divider = divider + j ))
done
for j in `grep $i input.txt | cut -f5 -d" "`
do
(( dividend = dividend + j ))
done
finalresult=$(echo "scale = 4; $dividend/$divider*100" | bc)
echo "$i"" ""$finalresult" >> result
done
  #4 (permalink)  
Old 11-22-2005
grasper grasper is offline
Registered User
  
 

Join Date: Sep 2005
Posts: 45
I don't know that the loop's the problem, but can't say for certain - could you exactly how it's not working? Is the expected output different from the actual result?
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:41 PM.


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