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
need top 3 numbers shary Shell Programming and Scripting 4 03-25-2008 01:58 AM
Question about adding numbers in unix dynastie UNIX for Dummies Questions & Answers 1 06-15-2006 03:26 PM
Adding a column of numbers Khoomfire UNIX for Advanced & Expert Users 1 01-18-2006 04:55 AM
adding float numbers strike Shell Programming and Scripting 5 08-29-2005 02:20 PM
Add some numbers! TalkShowHost Shell Programming and Scripting 3 05-15-2002 12:28 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 04-15-2004
Viper01 Viper01 is offline
Registered User
  
 

Join Date: Apr 2004
Posts: 2
Unhappy Adding 2 numbers

I would like to write a script with BASH to add two numbers (integer) and write the result to the standard output. Please help!
  #2 (permalink)  
Old 04-15-2004
google's Avatar
google google is offline Forum Advisor  
Moderator
  
 

Join Date: Jul 2002
Location: Atlanta
Posts: 740
use the bc command or use awk.
assuming two numbers in a file called file1

cat file1 | awk ' printf("%d\n", $1 + $2)'
  #3 (permalink)  
Old 04-15-2004
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,717
this short script works in sh & ksh, I assume bash as well.

Code:
let i=0
let i=$1+$2
echo $i
  #4 (permalink)  
Old 04-16-2004
home_king home_king is offline
Registered User
  
 

Join Date: Apr 2004
Location: China
Posts: 15
expr $var1 + $var2
  #5 (permalink)  
Old 04-16-2004
butterfm butterfm is offline
Registered User
  
 

Join Date: Sep 2003
Posts: 25
There are loads of ways to do it.

Another is

echo "1 + 2" | bc

Matt
  #6 (permalink)  
Old 04-17-2004
Viper01 Viper01 is offline
Registered User
  
 

Join Date: Apr 2004
Posts: 2
First of all, thank you very much for help, but I still have problems:

- I have to write my script without bc or dc commands.

- I would like to sum very-very big numers (600 characters long).
It doesn't work with let, expr, or awk. (because of overflow)

Have you got any idea, how to solve this problem?

Thank you!
  #7 (permalink)  
Old 04-17-2004
home_king home_king is offline
Registered User
  
 

Join Date: Apr 2004
Location: China
Posts: 15
Just try Gawk!
It performs in superior to other awk's clones.
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 08:05 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