如何判断在shell程序命令行输入的程序参数$1是否为空
(2010-08-04 16:37:56)标签: it |
本来一直使用:
DB_NAME=$1
if [ -z $DB_NAME ]
then
echo "database no selected yet "
DB_NAME=remotedb
else
echo "datebase selected yet "
echo "datebase selected yet DB_NAME=$DB_NAME"
fi
但是不能判断,一直显示不是空串,后面查询知道
问题跟用的shell有关。如果用ksh,直接用[ -z $1 ]或[[ -z $1 ]]就可以了,如果是sh,要把$1引起来:[ -z "$1" ]。另一个方法是[ ARG"$1" == ARG ]
修改为下面,OK了
DB_NAME=$1
if [ -z "$DB_NAME" ]
then
echo "database no selected yet "
DB_NAME=remotedb
else
echo "datebase selected yet "
echo "datebase selected yet DB_NAME=$DB_NAME"
fi