-2

piped eval in bash for string length comparison

i am trying to check if a certain device with a given id is plugged in and trigger a action based on that

i tried eval / exec

here is what i have so far

#!/bin/bash
KBP='[["lsusb -d 1c11:b04d | wc -c" == "0"]]'
if eval $KBP; then
    echo expression evaluated as true
else
    echo expression evaluated as false
fi

expected result:

if device is plugged in and string is not 0 it would hop in the false condition

actual result - cant evaluate the piped condition

Pierre
  • 21
  • 4

1 Answers1

1

Guessing fixed expression would look like this:

if [ "$(lsusb -d 1c11:b04d | wc -c)" -eq 0 ]; then

To remember:

  • Bash is spaces aware. [[ and ]] needs after and behind (well, ; is special here, it separates commands).
  • To get output of a command use command substitution $( ... )
  • There is no need for eval here.
KamilCuk
  • 69,546
  • 5
  • 27
  • 60