比较 sh 中的字符串

比较 sh 中的字符串

这是我尝试过的:

#! /bin/sh
    if [ -z "${ENCRYPT_ALL_PWD}" ]; then
        if [ ! -z $SA_USER ]; then
            DBAUSER=$SA_USER
            $ECHO "Yahoo!!!!!!!!!!!!"
         else
            $ECHO "Please define variable SA_USER in $HOME/<blah>profile and run this script again..."
            exit 1
         fi
    fi

但是当 ENCRYPT_ALL_PWD 未定义时,我无法摆脱输出中的“未找到”:

$ sh -x swar_test.sh
+ [  != YES ]
+ [ ! -z ]
+ Please define variable SA_USER in <blah>/<blah>profile and run this script again...
swar_test.sh[7]: Please define variable SA_USER in <blah>/<blah>profile and run this script again...:  **not found**
+ exit 1

我也尝试过这个,但它也不起作用,并且当 ENCRYPT_ALL_PWD 未定义/空时给出“未找到”:

#! /bin/sh
if [ "${ENCRYPT_ALL_PWD}" != "YES" ]; then
    if [ ! -z $SA_USER ]; then
        DBAUSER=$SA_USER
        $ECHO "Yahoo!!!!!!!!!!!!"
     else
        $ECHO "Please define variable SA_USER in $HOME/<blah>profile and run this script again..."
        exit 1
     fi
fi
exit 0

我在这里做错了什么?如何摆脱“未找到”?

我的期望是,当 ENCRYPT_ALL_PWD 未定义/空,并且 SA_USER 也未定义时,输出应该是:

Please define variable SA_USER in $HOME/<blah>profile and run this script again...

答案1

这设置正确:

$ cat swar_test.sh
    #! /bin/sh

    . $HOME/tools/init.sh
    . $HOME/<blah>profile
    if [ "${ENCRYPT_ALL_PWD}" != "YES" ]; then
        if [ ! -z $SA_USER ]; then
            DBAUSER=$SA_USER
            $ECHO "Yahoo!!!!!!!!!!!!"
         else
            $ECHO "Please define variable SA_USER in $HOME/aaaprofile and run this script again..."
            exit 1
         fi
    fi
    exit 0

相关内容