-3
# Detect Operating System
function dist-check() {
  # shellcheck disable=SC1090
  if [ -e /etc/os-release ]; then
    # shellcheck disable=SC1091
    source /etc/os-release
    DISTRO=$ID
    # shellcheck disable=SC2034
    DISTRO_VERSION=$VERSION_ID
  fi
}

# Check Operating System
dist-check

# Pre-Checks
function installing-system-requirements() {
  # shellcheck disable=SC2233,SC2050
  if ([ "$DISTRO" == "ubuntu" ] || [ "$DISTRO" == "debian" ] || [ "DISTRO" == "raspbian" ]); then
    apt-get update && apt-get install iptables curl coreutils bc jq sed e2fsprogs -y
  fi
  # shellcheck disable=SC2233,SC2050
  if ([ "$DISTRO" == "fedora" ] || [ "$DISTRO" == "centos" ] || [ "DISTRO" == "rhel" ]); then
    yum update -y && yum install epel-release iptables curl coreutils bc jq sed e2fsprogs -y
  fi
  if [ "$DISTRO" == "arch" ]; then
    pacman -Syu --noconfirm iptables curl bc jq sed
  fi
}

# Run the function and check for requirements
installing-system-requirements

Why wont this run?

I am using || to separate the distros but its still not working.

4
  • 3
    Which shell are you scripting for? ...also, your 3rd checks are missing $ on DISTRO. Commented Oct 31, 2020 at 2:06
  • 2
    ... this looks like something that could be more simply written as a case statement Commented Oct 31, 2020 at 2:15
  • 1
    If you haven't been disabling all shellcheck findings, you would be able to detect this. 😏 Commented Oct 31, 2020 at 2:33
  • have you run it through shellcheck? Are you doing one change at a time, and testing? It so then the bug will be in the last thing that you changed. Commented Oct 31, 2020 at 9:04

2 Answers 2

3

Some other notes:

function installing-system-requirements() is not standard syntax to define functions. The Bourne/POSIX syntax is installing-system-requirements() compound-command, function installing-system-requirements { ...; } is the Korn syntax. Combining both like that just works (mostly by accident) in pdksh, zsh and bash (though some shells like busybox sh and yash have added support for that since for compatibility with bash).

(...) is to introduce a subshell environment, which in most shells is implemented by forking a child shell process. You'd only use that to sort of isolate a section of code for changes in there not to be persistent. It doesn't make much sense to use them as you do here.

The equality comparison for the [ utility is =, not == (though some [ implementations understand == as well as an extension).

Here, you have a number of independent if statements. That means that if $DISTRO already matched debian for instance, you would still try and compare it to fedora. To avoid that, you could use elif for the subsequence checks:

if [ "$DISTRO" = debian ] || [ "$DISTRO" = ubuntu ]; then
  ...
elif [ "$DISTRO" = fedora ] || [ "$DISTRO" = centos ]; then
  ...
fi

But to match a string against several values or patterns, using a case construct would make it a lot easier:

case "$DISTRO" in
  (ubuntu | debian) ...;;
  (ferdora | centos) ...;;
esac
0
# Detect Operating System
function dist-check() {
  # shellcheck disable=SC1090
  if [ -e /etc/os-release ]; then
    # shellcheck disable=SC1091
    source /etc/os-release
    DISTRO=$ID
    # shellcheck disable=SC2034
    DISTRO_VERSION=$VERSION_ID
  fi
}

# Check Operating System
dist-check

# Pre-Checks
function installing-system-requirements() {
  # shellcheck disable=SC2233,SC2050
  if ([ "$DISTRO" == "ubuntu" ] || [ "$DISTRO" == "debian" ] || [ "$DISTRO" == "raspbian" ]); then
    apt-get update && apt-get install iptables curl coreutils bc jq sed e2fsprogs -y
  fi
  # shellcheck disable=SC2233,SC2050
  if ([ "$DISTRO" == "fedora" ] || [ "$DISTRO" == "centos" ] || [ "$DISTRO" == "rhel" ]); then
    yum update -y && yum install epel-release iptables curl coreutils bc jq sed e2fsprogs -y
  fi
  if [ "$DISTRO" == "arch" ]; then
    pacman -Syu --noconfirm iptables curl bc jq sed
  fi
}

# Run the function and check for requirements
installing-system-requirements

Was missing $ from DISTRO

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.