什么意思`!-d` 在这个 Bash 命令中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37403759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the meaning of `! -d` in this Bash command?
提问by Pullingmyhairout
Simple question, I don't understand what the !
and -d
in the below statement means.
简单的问题,我不明白下面陈述中的!
和-d
是什么意思。
if [ ! -d $directory ]
回答by
-d
is a operator to test if the given directory exists or not.
-d
是用于测试给定目录是否存在的运算符。
For example, I am having a only directory called /home/sureshkumar/test/.
例如,我只有一个名为 /home/sureshkumar/test/ 的目录。
The directory variable contains the "/home/sureshkumar/test/"
目录变量包含“/home/sureshkumar/test/”
if [ -d $directory ]
This condition is true only when the directory exists. In our example, the directory exists so this condition is true.
仅当目录存在时,此条件才成立。在我们的示例中,目录存在,因此此条件为真。
I am changing the directory variable to "/home/a/b/". This directory does not exist.
我正在将目录变量更改为“/home/a/b/”。该目录不存在。
if [ -d $directory ]
Now this condition is false. If I put the !
in front if the directory does not exist, then the if condition is true. If the directory does exists then the if [ ! -d $directory ]
condition is false.
现在这个条件是假的。如果我把!
目录不存在放在前面,那么if条件为真。如果目录确实存在,则if [ ! -d $directory ]
条件为假。
The operation of the ! operator is if the condition is true, then it says the condition is false. If the condition is false then it says the condition is true. This is the work of ! operator.
的操作!运算符是如果条件为真,则表示条件为假。如果条件为假,则表示条件为真。这是工作!操作员。
if [ ! -d $directory ]
This condition true only if the $directory does not exist. If the directory exists, it returns false.
仅当 $directory 不存在时,此条件才成立。如果目录存在,则返回 false。
回答by jDo
The brackets are the test executable, the exclamation mark is a negation, and the -d
option checks whether the variable $directory
is a directory.
括号是测试可执行文件,感叹号是否定,-d
选项检查变量$directory
是否是目录。
From man test:
从人测试:
-d FILE
FILE exists and is a directory
! EXPRESSION
EXPRESSION is false
The result is an if statement saying "if $directory
is not a directory"
结果是一个 if 语句,说“如果$directory
不是目录”
回答by Jeff Puckett
!
means not
!
意味着不
-d
means test if directory exists
-d
表示测试目录是否存在
So, if [ ! -d $directory ]
means if $directory
does not exist, or $directory isn't a directory (maybe a file instead).
因此,if [ ! -d $directory ]
意味着 if$directory
不存在,或者 $directory 不是目录(可能是文件)。
Usually this is followed by a statement to create the directory, such as
通常这后面跟着一条语句来创建目录,比如
if [ ! -d $directory ]; then
mkdir $directory
fi
回答by Suku
-d
is a test operator in bash and when you put !
before test operator - its negating the same
-d
是 bash 中的测试运算符,当您放在!
测试运算符之前时- 它否定相同
回答by akshat
!
negates the condition.-d
option checks$directory
is a directory or not.
!
否定条件。-d
选项检查$directory
是否是目录。