gpt4 book ai didi

unix - If 条件中的 -f 和 -s 选项有什么区别?

转载 作者:行者123 更新时间:2023-12-03 23:54:08 25 4
gpt4 key购买 nike

-f有什么区别和 -s If条件中的选项

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

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

最佳答案

来自 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

关于unix - If 条件中的 -f 和 -s 选项有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19856956/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com