-f
If条件下和-s
Option有什么区别
主要是它们在功能上有何不同?
我知道这些的谷歌含义,但想知道实际用途。
来自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