# 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为什么这次跑不了?
我正在使用||来分离发行版,但它仍然不能工作。
发布于 2020-10-31 07:53:42
其他一些注意事项:
function installing-system-requirements()不是定义函数的标准语法。Bourne/POSIX语法是installing-system-requirements() compound-command,function installing-system-requirements { ...; }是Korn语法。像这样的组合在pdksh、zsh和bash中只起作用(主要是偶然的)(虽然一些shell,比如busybox sh和yash已经增加了对它的支持,以便与bash兼容)。
(...)将引入一个子shell环境,在大多数shell中,该环境是通过分叉子shell进程来实现的。您只会使用它来隔离一段代码,以避免其中的更改持久存在。你在这里使用它们没有多大意义。
[实用程序的相等比较是=,而不是== (尽管有些[实现理解==以及扩展)。
在这里,有许多独立的if语句。这意味着,如果$DISTRO已经匹配了debian,那么您仍然可以尝试将其与fedora进行比较。为了避免这种情况,您可以使用elif进行子序列检查:
if [ "$DISTRO" = debian ] || [ "$DISTRO" = ubuntu ]; then
...
elif [ "$DISTRO" = fedora ] || [ "$DISTRO" = centos ]; then
...
fi但是,要将字符串与多个值或模式匹配,使用case构造将使其更加容易:
case "$DISTRO" in
(ubuntu | debian) ...;;
(ferdora | centos) ...;;
esac发布于 2020-10-31 02:21:28
# 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$从DISTRO失踪了
https://unix.stackexchange.com/questions/617275
复制相似问题