如何在脚本中传递空字符串

时间:2013-09-09 08:41:19

标签: shell sh

如何使用脚本传递空字符串。 as agqmi start 0“”“”“”。如果它无法在配置文件中找到设置。并且应用程序不是通过脚本调用的。但是通过命令行工作(agqmi start 0“”“”“”)。

profile_file

APN='airtelgprs.com'

USR='username'

PASS='password'

PAPCHAP='2'



if [ -f "$PROFILE_FILE" ]; then                                       

 echo "Loading profile..." >>$LOG                                

PAPCHAP=`cat agqmi-network.conf | grep 'PAPCHAP' | awk '{print $1}' | cut -f2 

-d"'"`                                                                                           
APN=`cat agqmi-network.conf | grep 'APN' | awk '{print $1}' | cut -f2 -d"'"`

USR=`cat agqmi-network.conf | grep 'USR' | awk '{print $1}' | cut -f2 -d"'"`

PASS=`cat agqmi-network.conf | grep 'PASS' | awk '{print $1}' | cut -f2 -d"'"`


if [ "x$PAPCHAP" == "x" ]; then
PAPCHAP="0"
fi

if [ "x$APN" == "x" ]; then
APN="\"\""
fi

if [ "x$USR" == "x" ]; then
USR="\"\"" 
fi

if [ "x$PASS" == "x" ]; then
PASS="\"\""                                                                                     
fi                                                                                                      

fi

我试图执行

    STATUS_CMD="./agqmi start "$PAPCHAP" "$APN" "$USR" "$PASS""    
    echo "$STATUS_CMD" >>$LOG                                                      
    `$STATUS_CMD`

2 个答案:

答案 0 :(得分:1)

以首先存储命令的方式运行命令的方法是通过这个(使用数组):

STATUS_CMD=(./agqmi start "$PAPCHAP" "$APN" "$USR" "$PASS")
echo "${STATUS_CMD[*]}" >>$LOG
"${STATUS_CMD[@]}"

你也可以使用eval但它可能会误解它,具体取决于变量的值。

您可能不再需要将您想要变空的变量重新分配给""(字面值)。只有需要转换为0的那个:

if [ "x$PAPCHAP" == "x" ]; then
    PAPCHAP="0"
fi
#if [ "x$APN" == "x" ]; then
#    APN="\"\""
#fi
#if [ "x$USR" == "x" ]; then
#    USR="\"\""
#fi
#if [ "x$PASS" == "x" ]; then
#    PASS="\"\""
#fi

您的比较不需要像x那样的标记。建议使用[[ ]]

if [[ $PAPCHAP == '' ]]; then  ## Or simply [[ -z $PAPCHAP ]]
    PAPCHAP=0
fi

POSIX更新:

if [ -z "$PAPCHAP" ]; then
    PAPCHAP=0
fi
#if [ -z "$APN" ]; then
#    APN=''
#fi
#if [ -z "$USR" ]; then
#    USR=''
#fi
#if [ -z "$PASS" ]; then
#    PASS=''
#fi

STATUS_CMD="./agqmi start \"$PAPCHAP\" \"$APN\" \"$USR\" \"$PASS\""
echo "$STATUS_CMD" >>"$LOG"
./agqmi start "$PAPCHAP" "$APN" "$USR" "$PASS"  ## Just execute it directly and not inside a variable.

也许你不应该添加./

STATUS_CMD="agqmi start \"$PAPCHAP\" \"$APN\" \"$USR\" \"$PASS\""
echo "$STATUS_CMD" >>"$LOG"
agqmi start "$PAPCHAP" "$APN" "$USR" "$PASS"

您实际上无需将其存储在变量上:

echo "agqmi start \"$PAPCHAP\" \"$APN\" \"$USR\" \"$PASS\"" >>"$LOG"
agqmi start "$PAPCHAP" "$APN" "$USR" "$PASS"

答案 1 :(得分:0)

您是否尝试将“\ 0”作为参数传递?

我有一个solaris服务器,每当我需要传递NULL字符串作为参数时,我使用“\ 0”。

您的命令看起来像

agqmi start 0 "\0" "\0" "\0"

如果它适合您,请告诉我。