linux - 不能在 bash if 语句中使用 'sudo'

标签 linux bash

我有双 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/

相关文章:

linux - 将 while 循环传递给超时

我可以从 popen() 流中打开 bash 吗?

linux - Bash 的段错误消息中的数字是什么意思?

linux - 从文件名 bash 脚本中去除前导点

bash - Colorized grep——查看整个文件并突出显示匹配项

linux - Ping on bash 脚本,ping 如果失败则退出脚本

python - django-admin 版本应该与 Django 相同吗?

c++ - 使用 pthread_kill() 终止因 I/O 而阻塞的线程的同步问题

linux - 我可以在共享主机服务器上安装 git 吗?

java - 未捆绑 JRE 的 Mac 和 Windows 打包 Jar 文件