可以在命令中插入单词吗?

可以在命令中插入单词吗?

你好,我正在尝试做一个兼容不同 Linux 发行版的安装程序。可以实现这样的功能(参见最后一行代码)。

PACKAGE_MANAGER=$1
INSTALL_PACKAGE_CMD=""

# CHECK PACKAGE MANAGER
if [ "$s1" == apt ]
then INSTALL_PACKAGE_CMD="apt install -y" fi
if [ "$s1" == pacman ]
then INSTALL_PACKAGE_CMD="pacman --noconfirm --force" fi

# INSTALL PACKAGE 
# (using the selected package manager)
sudo $INSTALL_PACKAGE_CMD sqlite

答案1

与 C 不同,bash 不会忽略行尾。试试这个:

s1=$1
INSTALL_PACKAGE_CMD=""

# CHECK PACKAGE MANAGER
if [ "$s1" == apt ] ; then
    INSTALL_PACKAGE_CMD="apt install -y" 
fi
if [ "$s1" == pacman ] ; then 
    INSTALL_PACKAGE_CMD="pacman -S --noconfirm --force"
fi

# INSTALL PACKAGE 
sudo $INSTALL_PACKAGE_CMD sqlite

相关内容