0

So there is a kernel adiutor for android, that let's you add custom controls with shell scripting. I'm trying to add a switch, but I have trouble setting the switch up correctly, when it's active, and when it's not. So there is a (text)file I'm trying to read (you will see it in the code), whether it's 0 or 1 inside, and that determines the switch on-off state.

I've tried with cat, read, everything, but honestly I think the problem is that I'm not familiar with sh scripting, and there is a problem with my syntax. Sometimes the script won't return anything when using cat. Also, su is available so that's not a problem, also the file has the correct permissions.

#!/system/bin/sh
var= $(<sys/class/lcd/panel/mdnie/hdr)
if (  "$var" = 0) then
    echo 0
    else echo 1
fi

The problem with my code is that right now it returns 1 (on), even when the file has a 0.

kotn3l
  • 3
  • 1

1 Answers1

0

When assigning a variable in shell, there must be no space after the assignment sign. Also, make sure you use the correct syntax for conditions (and be aware of whitespace sensitivity):

var=$(cat sys/class/lcd/panel/mdnie/hdr)
if [ "$var" = "0" ]; then
# if [ "$var" -eq 0 ], if you want numeric comparison (won't really matter here)
    echo 0
else
    echo 1
fi
knittl
  • 197,664
  • 43
  • 269
  • 318
  • @tripleee not in `bash` or `zsh` :) but I have adapted the script to work in any POSIX shell – knittl Aug 30 '19 at 11:15
  • Sorry, I meant to say this is not `sh` but would work in Bash or Zsh. See https://stackoverflow.com/questions/7427262/how-to-read-a-file-into-a-variable-in-shell – tripleee Aug 30 '19 at 11:15