“if [ -e $name ]”是什么意思?其中 $name 是目录的路径

use*_*585 10 shell scripting shell-script

if [ -e $name ],什么条件检查-e
$name是目录路径的变量。

phe*_*mer 14

-e如果目标存在,则返回 true。不管它是文件、管道、特殊设备,还是什么。某些东西可能存在并-e返回 false的唯一条件是符号链接损坏。

例如:

$ ln -s foo bar

$ [ -e foo ]; echo $?
1

$ touch bar

$ [ -e foo ]; echo $?
0
Run Code Online (Sandbox Code Playgroud)

在 bash 中,您可以help test查看您有哪些测试选项。


[通常是外壳的一部分。在 bash 中,选项和行为由 bash 定义。它也是 的同义词test。在 bash 中,您可以help test查看它支持的所有选项。和应该
之间唯一真正的区别是需要在您的论点之后,而不需要。否则它们的工作方式相同,相当于.[test[]test[ -e foo ]test -e foo

也有/usr/bin/[没有[内置的shell 。不过没有这方面的手册页。但是还有一个/usr/bin/test,我的系统确实有一个man test涵盖选项。我还没有测试过,但我敢打赌,/usr/bin/test工作支持的所有选项/usr/bin/[