1

I am attempting to make a simple script to flip my computer screen. My first if statement executes perfectly, but after that, thought $istouch is "on", the elif statement block will not execute! I have tried running bash with the -u option with no failure and have tried switching the checks, nothing works.

#!/bin/bash
xsetwacom --set 16 touch False
istouch='xsetwacom --get 15 touch'
$istouch
if [ "$istouch"=="off" ]
then
    xrandr -o inverted
    xinput set-prop 13 'Coordinate Transformation Matrix' -1 0 1 0 -1 1 0 0 1
    xinput set-prop 15 'Coordinate Transformation Matrix' -1 0 1 0 -1 1 0 0 1
    xinput set-prop 16 'Coordinate Transformation Matrix' -1 0 1 0 -1 1 0 0 1
    xsetwacom --set 15 touch True

elif [ "$istouch"=="on" ]
then
    xrandr -o 0
    xinput set-prop 13 'Coordinate Transformation Matrix' 1 0 0 0 1 0 0 0 1
    xinput set-prop 15 'Coordinate Transformation Matrix' 1 0 0 0 1 0 0 0 1
    xinput set-prop 16 'Coordinate Transformation Matrix' 1 0 0 0 1 0 0 0 1
    xsetwacom --set 15 touch False
fi
0
5

Your if statments aren't running the way that you think. You can turn up the debugging for Bash scripts like this by including the command set -x and subsequently turn it off with set +x.

Example

So we first add debugging like so:

#!/bin/bash

## DEBUG
set -x
xsetwacom --set 16 touch False
....

I then run your script, I called it ex.bash, so I invoke it:

$ ./ex.bash

Bash tries to execute this line:

if [ "$istouch"=="off" ]

And from the output, we can see that Bash is getting confused. It's running with the string 'xsetwacom --get 15 touch==off'.

+ '[' 'xsetwacom --get 15 touch==off' ']'

The arguments to the == shouldn't be touching it like that. Bash is notoriously picky on things like this. So put some space before and after like this:

 if [ "$istouch" == "off" ]
 elif [ "$istouch" == "on" ]

So now this looks a bit better:

+ '[' 'xsetwacom --get 15 touch' == off ']'
+ '[' 'xsetwacom --get 15 touch' == on ']'

However you don't want to compare the stirng $istouch, you want to compare the results of the command that this string represents, so change the top of the script to this:

....
xsetwacom --set 16 touch False
istouch=$(xsetwacom --get 15 touch)
if [ "$istouch" == "off" ]
....

Now we're running the command xsetwacom and storing the results in $istouch. I do not have these devices so I get a message about device 15. But this is what the script does now:

++ xsetwacom --get 15 touch
+ istouch='Cannot find device '\''15'\''.'
+ '[' 'Cannot find device '\''15'\''.' == off ']'
+ '[' 'Cannot find device '\''15'\''.' == on ']'

Hopefully this gives you some insight into:

  1. How to debug your script
  2. Better understanding of the Bash syntax

More indepth look at the if statements

You might be left wondering why the if statement even matched at all. The problem is that if you give the [ command a single string, it will treat this as a truth if the string is not empty, and the if statement will fall into its then section.

Example

$ [ "no"=="yes" ] && echo "they match"
they match

$ [ "notheydont"=="yes" ] && echo "they match"
they match

It may appear that there is a equality check occurring here, but there is not. [ some-string ] is short for [ -n some-string ], that is a test for some-string being [n]on-empty. Using set -x shows us this:

$ set -x; [ "notheydont"=="yes" ] && echo "they match"; set +x
+ '[' notheydont==yes ']'
+ echo 'they match'
they match
+ set +x

If we put some space between the arguments to the equality check:

# fails
$ set -x; [ "notheydont" == "yes" ] && echo "they match"; set +x
+ '[' notheydont == yes ']'
+ set +x

# passes
$ set -x; [ "yes" == "yes" ] && echo "they match"; set +x
+ set -x
+ '[' yes == yes ']'
+ echo 'they match'
they match
+ set +x

It now works as expected!

3
  • Ahh, thank you very much, it helps me a lot to know about the -x feature. However, one thing I don't understand is that in my version of the script it would (properly) flip the screen into tablet mode whenever I run it the first time. Why did the "$istouch"=="off" result to True?
    – Timidger
    Sep 24 '13 at 3:32
  • @Timidger See updates.
    – slm
    Sep 24 '13 at 3:42
  • @StephaneChazelas - thanks for the corrections!
    – slm
    Sep 24 '13 at 11:28
0

replace

istouch='xsetwacom --get 15 touch'
$istouch

with (note the back-quotes):

istouch=`xsetwacom --get 15 touch`

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.