Solved

calculation in a bash script

Posted on 2006-06-29
6
3,835 Views
Last Modified: 2010-08-05
Haven't been scripting under unix for a long time... many years
Can someone remind me how to calculate 2 + 3 and store the result in a variable.
Must be something with eval and/or bc, but can't find what it is anymore
0
Comment
Question by:lesouef
  • 3
  • 3
6 Comments
 
LVL 4

Accepted Solution

by:
JulieBouillon earned 100 total points
ID: 17009633
Hi,

SUM=$((2+3))
echo $SUM
0
 
LVL 28

Author Comment

by:lesouef
ID: 17010753
works. just wondering why we need double (())...

another point if I wanna check if $sum < 10, what's the right syntax? if I use <, it looks for a file to redirect. if I use LEQ, it does not understand...
since i've been javascript, it has confused me more! and the man is really on outdated tool. so am I probably!
0
 
LVL 4

Expert Comment

by:JulieBouillon
ID: 17012175
You need $(()) because using $(command) is the same as using `command`

$sum < 10
$sum -lt 10

Just a few more operators:
lt -> less than
gt -> greater than
eq -> equal
ne -> equal
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 28

Author Comment

by:lesouef
ID: 17013314
strange, this was I had done in between.
when using
 if [ $sum < 10 ] ; then etc....
I get in insult something like file not found (10)??
0
 
LVL 4

Expert Comment

by:JulieBouillon
ID: 17013798
It should have beem more explicit:
it is not -> $sum < 10
but -> $sum -lt 10
0
 
LVL 28

Author Comment

by:lesouef
ID: 17015857
ok, works. many thanks.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In this article we will discuss all things related to StageFright bug, the most vulnerable bug of android devices.
I use more than 1 computer in my office for various reasons. Multiple keyboards and mice take up more than just extra space, they make working a little more complicated. Using one mouse and keyboard for all of my computers makes life easier. This co…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now