'if -e'和'if -f'之间的区别 [英] Difference between 'if -e' and 'if -f'
问题描述
在if
条件下有两个用于检查文件的开关:-e
和-f
.
There are two switches for the if
condition which check for a file: -e
and -f
.
这两者之间有什么区别?
What is the difference between those two?
推荐答案
请参阅:我相信这些不是"if开关",而是测试开关"(因为您必须在[]括号内使用它们.
I believe those aren't "if switches", rather "test switches" (because you have to use them inside [] brackets.
但是区别是:
[ -e FILE ]
如果FILE存在,则为true.
[ -e FILE ]
True if FILE exists.
这将为/etc/hosts
和/dev/null
以及目录返回 true .
This will return true for both /etc/hosts
and /dev/null
and for directories.
[ -f FILE ]
如果FILE存在且为常规文件,则为true.
对于/etc/hosts
,这将返回 true ,对于/dev/null
,将返回 false (因为它不是常规文件),对于
[ -f FILE ]
True if FILE exists and is a regular file.
This will return true for /etc/hosts
and false for /dev/null
(because it is not a regular file), and false for /dev
since it is a directory.
这篇关于'if -e'和'if -f'之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!