ThePretender73
ThePretender73

Reputation: 35

Arithmetic statements in bash if clause

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

Answers (2)

chepner
chepner

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

gniourf_gniourf
gniourf_gniourf

Reputation: 46833

Use Bash arithmetic:

if (( value1<value2 && value1>=value2/2 )); then

Upvotes: 4

Related Questions