两个字符串之间的比较在shell脚本中不工作



这段代码将lsb_release -i命令的输出分配给linux_distro变量,脚本将其与distributor_id数组的第一项进行比较,然后与第二项进行比较,以此类推。数组的第一项是"Distributor ID: Arch",我使用arch,所以linux_distro变量应该等于数组的项。当然,自从我在这里发帖以来,情况似乎并非如此。

if [ "$linux_distro" = "${distributor_id[0]}" ] || [ "$linux_distro" = "${distributor_id[1]}" ]; then 
#checking if the yay AUR helper is available (it will be needed for some of the software installed)
if ! command -v snap > /dev/null; then 
echo "yay command was not found. Please install the yay AUR helper aur.archlinux.org/packages/yay from and try running the script again."
exit 1
fi
fi 

我尝试使用echo打印出变量值,这向我证明一切都应该按预期工作,我尝试使用[[命令和==操作符,仍然没有解决方案。

需要注意的是,我用snap代替了yay,只是因为我安装了yay而不是snap,所以请不要为那个烦恼。

我在你的代码中没有看到任何语法错误。因此,我猜这两个字符串实际上并不相等。在我的系统上,lsb_release -i的结果由制表符('t')分隔。也许你的另一个字符串被一个空格分隔。

另外,作为建议,您可以在脚本中获取/etc/os-release文件。然后,您可以简单地比较$ID变量的值,以确定分布。

您可以获取/etc/os-release的源代码

. /etc/os-release
if [ "$ID" = "${distributor_id[0]}" ] || [ "$ID" = "${distributor_id[1]}" ]; then 
#checking if the yay AUR helper is available (it will be needed for some of the software installed)
if ! command -v snap > /dev/null; then 
echo "yay command was not found. Please install the yay AUR helper aur.archlinux.org/packages/yay from and try running the script again."
exit 1
fi
fi 

此文件不存在于所有Linux发行版

最新更新