我有双 linux 引导我是 bash 的新手
运行以下脚本时出现奇怪的错误:
if [[ 'grep -i fedora /etc/issue' ]]; then
echo "the OS is Fedora"
$(sudo yum update -y && sudo yum upgrade -y)
else
echo "the OS is Ubuntu"
$(sudo apt-get update && sudo apt-get upgrade -y && sudo apt-get dist-upgrade -y)
fi
error : ./server_update.sh: line 9: Loaded: command not found
最佳答案
它正在尝试执行您的 apt-get/yum 命令的输出,丢失 $(..)
你一开始也有问题:
if [[ -n "$(grep -i fedora /etc/issue)" ]]; then
是检查字符串是否存在的正确方法。
您的代码应该如下所示:
if [[ -n "$(grep -i fedora /etc/issue)" ]]; then
echo "the OS is Fedora"
sudo yum update -y && sudo yum upgrade -y
else
echo "the OS is Ubuntu"
sudo apt-get update && sudo apt-get upgrade -y && sudo apt-get dist-upgrade -y
fi
关于linux - 不能在 bash if 语句中使用 'sudo',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23764938/