Phil Colson
Phil Colson

Reputation: 141

if statement numerical confusion in bash/sh

I've been arguing with this code all morning. It finally dawned on me, upon reading TL;DP that my if statements may be confusing output redirection with a comparison.

The code in question is:

#!/bin/sh
...
if [ $DEBUG_LEVEL > 2 ]
then
  echo "I made it here"
  echo "DEBUG: created run_all_somatic_SNV_steps" >>$LOG
fi

Is my if statement confusing stderr redirection with what I want it to do? (compare a variable to the number 2)

Upvotes: 0

Views: 128

Answers (2)

nullrevolution
nullrevolution

Reputation: 4137

try this test construct:

[ $DEBUG_LEVEL -gt 2 ]

Upvotes: 1

rici
rici

Reputation: 241771

For general shell:

if [ $DEBUG_LEVEL -gt 2 ]

(But that will fail if DEBUG_LEVEL has never been set.)

More bash-specific, and a lot nicer:

if (( DEBUG_LEVEL > 2 ))

Upvotes: 2

Related Questions