HiveBrain v1.2.0
Get Started
← Back to all entries
patternbashMinor

Script to toggle touchpad

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
scripttouchpadtoggle

Problem

This sequence properly shows whether the TouchpadOff option for synclient is on or not (outputs either a 0 or a 1):

synclient | grep 'TouchpadOff' | tail -n 1 | awk '{print substr($3, 1)}'


Trying to make a keyboard shortcut to toggle the touchpad with a basic script based on that line, I wrote this:

#!/bin/bash

PADOFF=$(synclient | grep 'TouchpadOff' | tail -n 1 | awk '{print substr($3, 1)}')
if [ "$PADOFF" == "0" ]
then
    synclient TouchpadOff=1
else
    synclient TouchpadOff=0
fi.

Solution

I don't understand the purpose of substr($3, 1) — isn't that the same as $3?

AWK alone can do the work of grep, tail, and negation. I would write it this way:

synclient TouchpadOff=$(synclient | awk '/TouchpadOff/ { off=int($3) }
                                         END           { print !off }')

Code Snippets

synclient TouchpadOff=$(synclient | awk '/TouchpadOff/ { off=int($3) }
                                         END           { print !off }')

Context

StackExchange Code Review Q#84355, answer score: 2

Revisions (0)

No revisions yet.