frostedhell
frostedhell

Reputation: 3

cannot 'sudo' inside of bash if statement

I've dual linux boot i'm newbie in bash

when running the following script i got strange error:

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

Upvotes: 0

Views: 416

Answers (1)

scragar
scragar

Reputation: 6824

It's attempting to execute the output of your apt-get/yum commands, lose the $(..)

You also have an issue at the start:

if [[ -n "$(grep -i fedora /etc/issue)" ]]; then

is the correct way to check if a string exists.

Your code should then look like this:

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

Upvotes: 2

Related Questions