0

I have the below Unix code:

#!/bin/bash

var="LogLevel"
LogLevel="WARN"

check="\$$var = \\"WARN\\""

if eval $check ; then
  echo "LogLevel contains WARN"
else
  echo "Contains something else"
fi

My objective is how do i do double variable comparison inside my if loop.

Is this the right way to do it ?

I am getting the below error:

./check.sh: line 8: WARN: command not found Contains something else

Kindly guide me with this issue .

Subhayan Bhattacharya
  • 5,407
  • 7
  • 42
  • 60
  • Why aren't you using a function? `check() { [[ ${!var} = WARN ]]; }; if check; then ...` – Charles Duffy Jan 12 '17 at 16:00
  • eval is running the string as if it was a command, there for it is running: `$LogLevel = \WARN\ ` use `set -x` at the begining of the script to see for yourself – Samuel Kirschner Jan 12 '17 at 16:11
  • BTW, `if` is not a loop. – Charles Duffy Jan 12 '17 at 16:11
  • 1
    @SamuelKirschner, ...to be fair, the OP *intends* to run the string as a command, except that they forgot the `test` or `[` that would be needed to make it a valid one. Not that they *should* be doing that -- [BashFAQ #50](http://mywiki.wooledge.org/BashFAQ/050) goes into the perils of executing strings as commands, and [BashFAQ #48](http://mywiki.wooledge.org/BashFAQ/048) discusses (some of) the reasons to avoid `eval`. – Charles Duffy Jan 12 '17 at 16:12

0 Answers0