yatici
yatici

Reputation: 577

compare as numeric value. unix

So I am trying to test a few things coding in bash. As a n00b I am having some problems with the basics and how irritating it can get using shell dealing with numbers.

if $((echo $?)) > 0 ;then 
  echo "there is an error"; 
else
  echo "it passed"; 
fi

the error I get is : bash: echo 0: syntax error in expression (error token is "0")

I tried without the echo and I get: bash: ./0: Permission denied

so I am a bit confused what the shell is trying to do.

Thanks

Upvotes: 1

Views: 2809

Answers (2)

Kent
Kent

Reputation: 195239

try this:

OUT=$?
if [ $OUT -eq 0 ];then
   echo "OK!"
else
   echo "NOT OK!"
fi

Upvotes: 0

Chewie
Chewie

Reputation: 7235

The whole arithmetic evaluation must be within double parentheses:

if (($? > 0)) ;then 
  echo "there is an error"; 
else
  echo "it passed"; 
fi

Upvotes: 2

Related Questions