语法检查。第二组if语句

时间:2012-10-09 19:13:55

标签: linux bash shell

if [ $DISTRO = "REDHAT"] || [ $DISTRO = "FEDORA"]; then 
    if ! rpm -qa | grep -q glibc-static; then
        $BIN_ECHO -e " Package [glibc-static] not found. Installing.. "
        yum install glibc-static
    elif
        $BIN_ECHO -e " All required packages installed.. "
    fi
elif [ $DISTRO = "DEBIAN"]; then 
    if ! dpkg-query -l glibc; then
        $BIN_ECHO -e " Package [glibc] not found. Installing.. "
        apt-get install glibc
    elif
        $BIN_ECHO -e " All required packages installed.. "
    fi
fi

第156行:意外标记“fi”附近的语法错误

如何将两个陈述放在一起?

1 个答案:

答案 0 :(得分:4)

每次测试的最终]之前必须有空格。

例如:

if [ $DISTRO = "REDHAT"] || [ $DISTRO = "FEDORA"]; then

必须重写为:

 if [ "$DISTRO" = REDHAT ] || [ "$DISTRO" = FEDORA ]; then

if test "$DISTRO" = REDHAT || test "$DISTRO" = FEDORA; then

另请注意,没有理由引用文字字符串,但您应引用变量。

你也可以这样做:

if test "$DISTRO" = REDHAT -o "$DISTRO" = FEDORA; then

case "$DISTRO" in
REDHAT|FEDORA) ... ;;
DEBIAN) ... ;;
esac