bash-newbie在这里。bash:比较字符串与grep-variable问题
我想用下面简单的脚本的快捷方式启用/禁用我的笔记本电脑的触控板:如果条件不过是从未进入
#!/bin/bash
result=$(xinput --list-props 11 | grep "Device Enabled")
echo $result
# Output: Device Enabled (140): 1
if [[ "$result" = "Device Enabled (140): 1" ]]; then
`xinput set-prop 11 "Device Enabled" 0`
else
`xinput set-prop 11 "Device Enabled" 1`
fi
的。 echo $result
显示该变量确实包含我想要比较的字符串值。我一直在寻找一段时间,但根本无法弄清楚为什么结果变量和字符串在if条件中不匹配。
请看看:http://www.shellcheck.net/ – Cyrus
用'echo“$ result”'替换'echo $ result'。变量中有两个选项卡。 – Cyrus
你真的有条件使用'xinput'包含在后面的引号中吗?如果你想显示这些命令的输出,你需要使用'echo'之类的东西来放在它们的前面。而且,因为您已经在使用cmd替换的'$(cmd)'形式,所以对这些行也这样做。我猜你并不需要cmd替换你的有条件使用'xinput ...',。将有兴趣看看'shellcheck.net'标志。 (是的,它的确如此)。祝你好运。 – shellter