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
 


 

Comments:

Post a Comment:
Comments are closed for this entry.
About

williamxue

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today