简体   繁体   English

在bash脚本中切换两个命令?

[英]Toggle between two commands in bash script?

I would like to use a simple bash script to toggle on/off of the touchpad of my laptop. 我想使用一个简单的bash脚本来打开/关闭我的笔记本电脑的触摸板。 I know that I can use synclient Touchpadoff=1 to turn off the touchpad and use synclient Touchpadoff=0 to turn it on. 我知道我可以使用synclient Touchpadoff=1关闭触摸板并使用synclient Touchpadoff=0将其打开。 But is there any way to toggle between these two commands (ie, if the touchpad is off, then turn it on, and vice versa)? 但有没有办法在这两个命令之间切换(即,如果触摸板关闭,然后将其打开,反之亦然)?

Thanks! 谢谢!

Doing synclient -l , you can see the line synclient -l ,你可以看到这一行

    TouchpadOff          = 0

(or =1) (或= 1)
You then just have to grep this value and call synclient Touchpadoff= with the other value. 然后你只需要grep这个值并调用synclient Touchpadoff=和另一个值。
For instance in a script toggleTouchpad.sh , you can have 例如,在脚本toggleTouchpad.sh ,您可以拥有

#!/bin/bash
synclient TouchpadOff=$(synclient -l | awk '/TouchpadOff/{print !strtonum($3)}')

the awk command will print the "opposite" of the third field ( = is the second), so if it's 1 it will print 0, if it's 0 it will print 1. awk命令将打印第三个字段的“反面”( =是第二个字段),所以如果它是1,它将打印0,如果它是0,它将打印1。

not very good, but easy to implement solution is to save in /tmp/touch.state actual state of touchpad. 不是很好,但易于实现的解决方案是保存在/tmp/touch.state触摸板的实际状态。
If it's '0' - enable touchpad and store '1' in file and vice versa 如果它为'0' - 启用触摸板并在文件中存储'1',反之亦然

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM