【发布时间】:2022-07-06 02:39:23
【问题描述】:
我正在尝试创建一个数组并使用 Homebrew 来安装应用程序。但在我安装应用程序之前,我想检查它是否已安装。我知道它已经存在于 Brew 中,但我正在查看类似这样的内容:
declare -a applications=(Spotify Discord Franz Rectangle visual-studio-code VLC microsoft-excel)
for i in "${applications[@]}"
do
#check for app installer
if [ -d "/Applications/$i.app" ]; then
echo " $i is installed"
appstatus="Installed"
else
echo "/Applications/$i.app"
appstatus=" $i, not installed - installing now"
brew install cask "$i"
fi
echo $appstatus
done`
但是,由于 - 不在应用程序文件夹的名称中,因此应用程序数组在 VSC 和 Excel 上总是会失败。
要么我要在下面创建另一个具有正确名称的数组 - 要么我想知道我是否可以解析数组并删除 - 以便我们检查应用程序是否已安装。
希望这是有道理的。
【问题讨论】:
-
array[$index]=newvalue-- 您可以使用"${!array[@]}"迭代索引 -
也就是说,应该叫什么名字?它应该是空格而不是破折号吗?为什么不首先将空格存储在数组中,而不是事后对其进行编辑?
-
declare -a applications=(Spotify Discord Franz Rectangle "visual studio code" VLC "microsoft excel") -
顺便说一句,
echo $appstatus应该是echo "$appstatus"。见I just assigned a variable, butecho $variableshows something else! -
...还是
brew install需要空格的问题?您可以仅针对该命令执行相反的操作:brew install cask "${application// /-}"