技术小黑屋

BashBites:Check Files

Check a file exists

1
2
3
4
5
6
7
#!/bin/bash
if [[ -e /tmp/adb.log  ]]
then
    echo "Exists"
else
    echo "Not Exists"
fi

Check Empty String

1
2
3
4
5
6
if [[ -z "$emptyString"  ]]
then
    echo "Empty"
else
    echo "Not Empty"
fi

Here is a reference material from Stackoverflowhttp://stackoverflow.com/questions/3767267/check-if-file-exists

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
-d FILE
    FILE exists and is a directory
-e FILE
    FILE exists
-f FILE
    FILE exists and is a regular file
-h FILE
    FILE exists and is a symbolic link (same as -L)
-r FILE
    FILE exists and is readable
-s FILE
    FILE exists and has a size greater than zero
-w FILE
    FILE exists and is writable
-x FILE
    FILE exists and is executable
-z STRING
    the length of STRING is zero

For more detailed information, please visit http://linux.about.com/library/cmd/blcmdl1_test.htm

Others


我的知乎 Live 推荐