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!
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.
Comments