Bash if [ -d $1] 为空 $1 返回 true

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21678771/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 09:31:52  来源:igfitidea点击:

Bash if [ -d $1] returning true for empty $1

bashif-statement

提问by fgysin reinstate Monica

So I have the following little script and keep wondering..

所以我有以下小脚本并不断想知道..

#!/bin/bash

if [ -d  ]; then
  echo 'foo'
else
  echo 'bar'
fi

.. why does this print foo when called without arguments? How is it that the test [-d ] returns true for an empty string?

.. 为什么在不带参数的情况下调用时会打印 foo ?测试 [-d ] 如何为空字符串返回 true?

采纳答案by fedorqui 'SO stop harming'

From: info coreutils 'test invocation'(reference found through man test):

来自:(info coreutils 'test invocation'通过 找到的参考man test):

If EXPRESSION is omitted, test' returns false. **If EXPRESSION is a single argument,test' returns false if the argument is null and true otherwise**. The argument can be any string, including strings like -d',-1', --',--help', and --version' that most other programs would treat as options. To get help and version information, invoke the commands[ --help' and `[ --version', without the usual closing brackets.

如果省略 EXPRESSION,test' returns false. **If EXPRESSION is a single argument,则如果参数为 null ,则test' 返回 false,否则返回 true**。参数可以是任何字符串,包括像-d',-1'、-- --',help' 和--version' that most other programs would treat as options. To get help and version information, invoke the commands[ --help' 和 `[ --version'这样的字符串 ,没有通常的右括号。

Highlighting properly:

正确突出显示:

If EXPRESSION is a single argument, `test' returns false if the argument is null and true otherwise

如果 EXPRESSION 是单个参数,如果参数为 null,`test' 返回 false,否则返回 true

So whenever we do [ something ]it will return trueif that somethingis not null:

所以每当我们这样做时[ something ]true如果它something不为空,它就会返回:

$ [ -d ] && echo "yes"
yes
$ [ -d "" ] && echo "yes"
$ 
$ [ -f  ] && echo "yes"
yes
$ [ t ] && echo "yes"
yes

Seeing the second one [ -d "" ] && echo "yes"returning false, you get the way to solve this issue: quote $1so that -dalways gets a parameter:

看到第二个[ -d "" ] && echo "yes"返回false,你就有了解决这个问题的方法:quote$1这样-d总是得到一个参数:

if [ -d "" ]; then
  echo 'foo'
else
  echo 'bar'
fi

回答by devnull

The reason that

原因是

[ -d ] && echo y

produces yis that the shell interprets it as a stringin the testcommand and evaluates it to true. Even saying:

产生y的是,外壳将其解释为一个字符串test命令,并将其评估为TRUE。甚至说:

[ a ] && echo y

would produce y. Quoting from help test:

会产生y. 引自help test

 string        True if string is not the null string.


That is why quoting variables is recommended. Saying:

这就是为什么推荐引用变量的原因。说:

[ -d "" ] && echo y

should not produce ywhen called without arguments.

y带参数调用时不应产生。

回答by Alfe

The reason is plain and simple: The syntax does not match the case in which the -dis recognized as an operator working on a file name. It is just taken as a string, and each non-empty string is true. Only if a second parameter to -dis given, it is recognized as the operator to find out whether a given FILE is a directory.

原因很简单:语法与将-d识别为处理文件名的运算符的情况不匹配。它只是作为一个字符串,每个非空字符串都为真。只有-d给定了第二个参数to ,才被认为是判断给定FILE是否为目录的运算符。

The same applies to all the other operators like -e, -r, etc.

这同样适用于所有其他运营商一样-e-r等等。

In your case, use double quotes to avoid running into that "problem":

在您的情况下,请使用双引号以避免遇到该“问题”:

[ -d "" ]