Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Arithmetic Operations

Status
Not open for further replies.

olli2003

Technical User
Jan 31, 2003
93
DE
Hi guys

Can someone of you tell me how to do arithmetic operations within a shell script?
For explaining, I've two output files with digits in it. Every file contains one digit. So now I'd like to add or subtract this two digits and want to create a new file with it's output. Is it possible and how?
Many thanks and best regards!
Oliver
 
Checkout 'expr' in the man pages

Sorry for only 1 example the rest of the page does not format very well :)

Alex


Example
To modify a shell variable, enter:
COUNT=`expr $COUNT + 1`
This adds 1 to the shell variable $COUNT. The expr command is enclosed in grave accents, which causes the shell to substitute the standard output from the expr command into the COUNT= command. The $COUNT variable must be initialized before using.
 
Assuming that the files only contain the digits to be added

expr $(cat file1) + $(cat file2) > file3

Dave
 
Hi Dave

That's what I wanted to know -
really easy if you know.
Thanks a lot and also to each others tried to help me!

Best Regards
Oliver
 
Hello

Can someone of you tell me how to do arithmetic operations with floating-point numbers within a shell script?

Dominik
 
hi,
Dominik

I use the command bc (that you can use from shell) as

X1=2.2222222222
X2=3.3333333333
XT=`echo "$X1 + $X2" | bc`
echo $XT

bye
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top