[英]Difference between 'if -e' and 'if -f'
There are two switches for the if
condition which check for a file: -e
and -f
.检查文件的if
条件有两个开关: -e
和-f
。
What is the difference between those two?这两者有什么区别?
See: http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html请参阅: http : //tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html
I believe those aren't "if switches", rather "test switches" (because you have to use them inside [] brackets.我相信那些不是“if 开关”,而是“测试开关”(因为你必须在 [] 括号内使用它们。
But the difference is:但不同的是:
[ -e FILE ]
True if FILE exists. [ -e FILE ]
如果 FILE 存在,则为真。
This will return true for both /etc/hosts
and /dev/null
and for directories.这将为/etc/hosts
和/dev/null
以及目录返回true 。
[ -f FILE ]
True if FILE exists and is a regular file. [ -f FILE ]
如果 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.这将为/etc/hosts
返回true ,为/dev/null
返回false (因为它不是一个常规文件),对于/dev
false ,因为它是一个目录。
$ man bash
-e file
True if file exists.
-f file
True if file exists and is a regular file.
A regular file is something that isn't a directory, symlink, socket, device, etc.常规文件不是目录、符号链接、套接字、设备等。
-e
checks for any type of filesystem object; -e
检查任何类型的文件系统对象; -f
only checks for a regular file. -f
只检查常规文件。
The if statement actually uses the program 'test' for the tests. if 语句实际上使用程序“test”进行测试。 You could write if statements two ways:您可以通过两种方式编写 if 语句:
if [ -e filename ];
or要么
if test -e filename;
If you know this, you can easily check the man page for 'test' to find out the meanings of the different tests:如果您知道这一点,您可以轻松查看“test”的手册页以找出不同测试的含义:
man test
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.