3

-fIf条件下和-sOption有什么区别

主要是它们在功能上有何不同?

我知道这些的谷歌含义,但想知道实际用途。

4

1 回答 1

9

来自man test

-f FILE
          FILE exists and is a regular file

-s FILE
          FILE exists and has a size greater than zero

例子

让我们从头开始创建一个文件并检查它。

$ touch b

文件是否存在?

$ [ -f "b" ] && echo "file exists"
file exists                          # yes!!!!

文件的大小是否大于零?

$ [ -s "b" ] && echo "file exists and is greater than zero"
$                                    # no!!!!

因此,检查文件是否存在的一个好if-elif-else条件可能是:

if [ -s "$file" ]; then
   echo "exists and it is not empty"
elif [ -f "$file" ]; then
   echo "at least exists"
else
   echo "does not exist"
fi
于 2013-11-08T10:39:09.787 回答