Bash: 'if' statement always seems to evaluate first condition as true even when false

superdude

I just started using bash recently and I'm having troubles with a simple command. Here's the script:

#!/bin/bash
if TouchpadOff=0; then 
    synclient TouchpadOff=1
    echo "Touchpad has been turned off"
else
    synclient TouchpadOff=0
    echo "Touchpad has been turned on"
fi

Basically, what I'm trying to do is toggle the pad to turn off or on depending on whether it is currently on or off. When the pad is on, running the script turns it off, which is what I want it to do.

But for some reason, when I run it again to turn the pad on, it is still turned off and still echos "Touchpad has been turned off", even when I want it to turn on. I hope someone can help me with my question. Thanks!

Byte Commander

The problem is your if-condition is wrong. It does not do what you think it does.

if TouchpadOff=0; then 

This runs TouchpadOff=0 as a shell command, which creates a variable $TouchpadOff and assigns it the value 0. As this should normally run successfully, the result is that the condition is always true.

You'll have to call the synclient command and parse its output to check the state of the TouchpadOff property, like e.g. this way:

if [[ "$(synclient | grep 'TouchpadOff' | awk '{print $3}')" -eq 0 ]] ; then

This will run synclient | grep 'TouchpadOff' | awk '{print $3}', which parses the output of synclient for a line containing "TouchpadOff" and returns the third column of that line.
It's inside a command substitution $( ... ), so that whole part will be substituted with the output of the inner command.
Then [[ ... -eq 0 ]] checks whether the substituted output is equal to 0 and makes that the condition for the if-statement.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Boolean condition is always false when using `status == true`

From Dev

passport's req.isAuthenticated always returning false, even when I hardcode done(null, true)

From Dev

Evaluate expression according to the first true condition

From Dev

Statement inside If block executing even when condition is false

From Dev

C: False condition is interpreted as true in if statement

From Dev

My code always returns true, even when it should be false

From Dev

Functions returns false even when the if statement is true

From Dev

Bash: Unable to enter into if even the condition is true

From Dev

Angular 2 - Custom validator always returns true, even when condition is false

From Dev

Compiler: What if condition is always true / false

From Dev

Why does my condition always evaluate as true?

From Dev

First if statement always true, but second one is not

From Dev

SonarLint : Change this condition so that it does not always evaluate to "true"

From Dev

SonarQube claims condition to always evaluate to false for fields accessed with "this."

From Dev

Evaluate expression according to the first true condition

From Dev

Can't get if statement to evaluate properly -- .include? always evaluates to false

From Dev

Checkbox is always returning false even when checked

From Dev

C++ GUI Condition for if statement always true

From Dev

Bash always evaluate Regex as true

From Dev

If statement running even when not true

From Dev

Bash Script if condition is always true

From Dev

vb.net IF statement nestled in another IF statement always will return true and never run the 'false' code even if the conditions point to false

From Dev

javascript form validation always returns true even when condition matches and should returns false

From Dev

Null-conditional operator always true in if statement when should be false

From Dev

The condition is TRUE even though it should be FALSE

From Dev

Javascript enters if statement when the condition should evaluate false

From Dev

Why does True == True evaluate to True, when {a statement that's equal to True} == True evaluates to false?

From Dev

Always include children even if condition is false

From Dev

Why is the first condition of "else if" statement always false?

Related Related

  1. 1

    Boolean condition is always false when using `status == true`

  2. 2

    passport's req.isAuthenticated always returning false, even when I hardcode done(null, true)

  3. 3

    Evaluate expression according to the first true condition

  4. 4

    Statement inside If block executing even when condition is false

  5. 5

    C: False condition is interpreted as true in if statement

  6. 6

    My code always returns true, even when it should be false

  7. 7

    Functions returns false even when the if statement is true

  8. 8

    Bash: Unable to enter into if even the condition is true

  9. 9

    Angular 2 - Custom validator always returns true, even when condition is false

  10. 10

    Compiler: What if condition is always true / false

  11. 11

    Why does my condition always evaluate as true?

  12. 12

    First if statement always true, but second one is not

  13. 13

    SonarLint : Change this condition so that it does not always evaluate to "true"

  14. 14

    SonarQube claims condition to always evaluate to false for fields accessed with "this."

  15. 15

    Evaluate expression according to the first true condition

  16. 16

    Can't get if statement to evaluate properly -- .include? always evaluates to false

  17. 17

    Checkbox is always returning false even when checked

  18. 18

    C++ GUI Condition for if statement always true

  19. 19

    Bash always evaluate Regex as true

  20. 20

    If statement running even when not true

  21. 21

    Bash Script if condition is always true

  22. 22

    vb.net IF statement nestled in another IF statement always will return true and never run the 'false' code even if the conditions point to false

  23. 23

    javascript form validation always returns true even when condition matches and should returns false

  24. 24

    Null-conditional operator always true in if statement when should be false

  25. 25

    The condition is TRUE even though it should be FALSE

  26. 26

    Javascript enters if statement when the condition should evaluate false

  27. 27

    Why does True == True evaluate to True, when {a statement that's equal to True} == True evaluates to false?

  28. 28

    Always include children even if condition is false

  29. 29

    Why is the first condition of "else if" statement always false?

HotTag

Archive