0

I am trying to read line by line from a serial port. The code below works, but I keep getting an error which I do not understand. Any clue?

That's the code

    while read line ; do
    if [ "$(line)"=='OFF' ] ;
      then
         echo "that was actually OFF"
    fi
    done < /dev/ttyACM0

That's the error:

   that was actually OFF 
   ./reading_loop.sh: line 9: line: command not found
3
  • 3
    $line or ${line} instead of $(line) Commented Aug 29, 2014 at 11:15
  • @Ronald why not post that as an answer? Commented Aug 29, 2014 at 11:48
  • @ssdecontrol: because it was so short. But you're right, it was actually an answer Commented Aug 30, 2014 at 11:23

1 Answer 1

1
if [ "$(line)"=='OFF' ] ;
  1. $(...) executes the contents of the parentheses and returns the stdout. If you don't have a command named line you'll see a "command not found" error message, and $() will return an empty string.
  2. [...] does different actions based on the number of arguments given. Since you only give a single word, the [ command merely checks if the string is empty or not. In this case, [ will see ""=='OFF' which is not empty, therefore the success block will always be executed.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.