Bash True/false if/then/else shell脚本
这是我的剧本:Bash True/false if/then/else shell脚本,bash,shell,Bash,Shell,这是我的剧本: #!/bin/bash result=$(xfconf-query -c xsettings -p /Gtk/ShellShowsMenubar) if $result == "false" then xfconf-query -c xsettings -p /Gtk/ShellShowsMenubar -n -t bool -s true else xfconf-query -c xsettings -p /Gtk/ShellShowsMenubar -n -
#!/bin/bash
result=$(xfconf-query -c xsettings -p /Gtk/ShellShowsMenubar)
if $result == "false"
then
xfconf-query -c xsettings -p /Gtk/ShellShowsMenubar -n -t bool -s true
else
xfconf-query -c xsettings -p /Gtk/ShellShowsMenubar -n -t bool -s false
fi
它不输出错误,但也不工作 您的线路:
if $result == "false"
尝试运行名为true
或false
的命令,这两个命令在bash中都是有效的关键字,因此不会出现错误。额外的参数==
和false
没有任何用处
运行true
返回一个“成功”退出代码,因此当$result
为true
时,您的代码将跟随if
分支,当false
时,它将跟随else
分支
要测试字符串的值,请使用标准语法:
if [ "$result" = false ]
或者特定于bash:
if [[ $result = false ]]
bash支持
=
,但您也可以在任何地方使用标准运算符=
。如果[$result==false],请尝试使用
请注意,每个字符之间都有空格,并且false
不在双引号内,请始终将脚本复制粘贴到以修复语法冲突。此时不显示输出?请注意,如果语句,您可能根本不需要xfconf query
有一个-T
选项来切换布尔设置。我没有安装它进行测试,因此我不确定1)如果您使用-t
,是否仍然需要-t bool
,或者2)如何-T
和-n
相互作用。请添加一个简短的解释。虽然这个答案可能确实解决了这个问题,但如果您解释为什么它解决了这个问题,以及为什么OP的原始方法不起作用,那将更有帮助。
xfconf-query -c xsettings -p /Gtk/ShellShowsMenubar -T