Shell Tips – how to check Null string and empty directory ?

Check Null string

#!/bin/sh
echo $0

if [ "$str" = "" ];then
    echo NULL string1
fi

if [  -z "$str" ]; then
    echo NULL string2
fi

if [ ! "$str" ]; then
    echo NULL string3
fi

[ "$str" ] || echo NULL string4

#If there is no "", it still can run!

[ $str ] || echo NULL string4

[ -z  "$str" ] && echo NULL string5

 

In bash, it seem that only string1 must have "".

Check empty directory

 [ `ls -al $Dir | wc -l` = 3 ] && echo Empty dir $Dir || echo Not empty dir $Dir
 

 

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s