linux shell 中判断字符串为空的正确方法

来源:互联网 发布:全站仪传输软件 编辑:程序博客网 时间:2024/05/29 17:32

原文地址:http://www.cnblogs.com/cute/archive/2011/08/26/2154137.html


help命令可以查看帮助

help test

 

正确做法:

 

#!/bin/sh

STRING=

if [ -z "$STRING" ]; then
    echo "STRING is empty"
fi

if [ -n "$STRING" ]; then
    echo "STRING is not empty"
fi

 

root@james-desktop:~# ./zerostring.sh
STRING is empty

-------------------------------------------------------------------------

错误做法:

#!/bin/sh

STRING=

if [ -z $STRING ]; then
    echo "STRING is empty"
fi

if [ -n $STRING ]; then
    echo "STRING is not empty"
fi 

输出错误结果:

root@james-desktop:~# ./zerostring.sh
STRING is empty
STRING is not empty


0 0
原创粉丝点击