Reputation: 35
Just a quick question about the above stated topic:
I'm currently trying to use a value i am given through read in my if statement:
if [ $value1 -lt $value2 -a $value1 is -ge $value2/2 ]
How is it possible to compare one value to half of another without having to make an equation beforehand?
Upvotes: 2
Views: 91
Reputation: 531275
The POSIX version (which also works in bash
, although clumsier than using an arithmetic statement) is
if [ "$value1" -lt "$value2" ] && [ "$value1" -ge $(( "$value2" / 2)) ]
(As an aside, don't use -a
for logical AND; it's an extension to the POSIX standard that isn't guaranteed to be implemented by all compliant shells.)
Upvotes: 0
Reputation: 46833
Use Bash arithmetic:
if (( value1<value2 && value1>=value2/2 )); then
Upvotes: 4