bash - 如何在 shell 脚本中为布尔变量编写 if then?
问题描述
我正在尝试编写一个简单的 shell 脚本来隐藏/显示我的桌面图标,但是当我尝试运行它时出现错误“hidedesktopicons.sh: line 1: [[gsettings: command not found”?
我搜索了如何在 shell 脚本中使用 if then 语句。我加了“fi”
enter code here
cat hidedesktopicons.sh
if [[gsettings get org.mate.background show-desktop-icons = true]]
then gsettings set org.mate.background show-desktop-icons false
else gsettings set org.mate.background show-desktop-icons true
fi
我希望图标隐藏/取消隐藏。我收到错误“hidedesktopicons.sh: line 1: [[gsettings: command not found”
解决方案
最直接的问题是你需要空格之后[[
:
if [[ gsettings ...
下一个问题是您需要使用命令替换来捕获的输出,gsettings
以便您可以将其与true
:
if [[ $(gsettings get org.mate.background show-desktop-icons) = true ]]
then gsettings set org.mate.background show-desktop-icons false
else gsettings set org.mate.background show-desktop-icons true
fi
您可能希望定义一对函数来减少此代码的一些重复性:
get_icon_status () {
gsettings get org.mate.background show-desktop-icons
}
set_icon_status () {
gsettings set org.mate.background show-desktop-icons "$1"
}
if [[ $(get_icon_status) = true ]]; then
set_icon_status false
else
set_icon_status true
fi
推荐阅读
- vb.net - LINQ - Group By - IEnumerable 返回
- javascript - Chrome 扩展 V3。Service Worker 没有 mediaDevices
- path-finding - 最短路径算法
- python - 使用其他列表中的项目在列表中搜索
- python - “tweepy”库中的 Python 错误语法
- bash - 如何将命令的输出通过管道传输到程序并保持其标准输入打开
- javascript - 页面刷新时反应为空状态
- reactjs - 使用标记库 - TypeError: mapped.parse is not a function
- antlr - 基本表达式 Antlr4 语法
- android - 在 AIR for Android 应用程序中在哪里调试此崩溃?