我写了这个脚本来切换我的触屏(我会把它键绑定),但是它不能工作。为什么这个高级代码不能工作?怎么才能更优雅地完成这个功能?
#!/bin/sh
if [ "synclient | grep TouchpadOff | grep -o -E '[0-9]+'" ]
then
synclient TouchpadOff=0
notify-send "Touchpad Enabled"
else
synclient TouchpadOff=1
notify-send "Touchpad Disabled"
fi发布于 2019-06-29 01:44:17
当你写
if [ "some string or other" ]
then[运算符不运行some string or other,它只看到一个非空字符串,并表示这是一个true值。
你想要的
if [ "`synclient | grep TouchpadOff | grep -o -E '[0-9]+'`" ] 或者说更现代
if [ "$(synclient | grep TouchpadOff | grep -o -E '[0-9]+')" ]但是,实际上不需要查看命令是否像grep生成退出代码那样生成字符串。
if synclient | grep TouchpadOff | grep -q -E '[0-9]+'
then应该做你想做的事。我不知道synclient的输出格式,但我希望您可以将这两个grep合并为一个,而无需花费太多的精力。
https://unix.stackexchange.com/questions/527585
复制相似问题