bash 中使用的 If [[-n variable ]] 语法是什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19628973/
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 If [[-n variable ]] syntax used for in bash
提问by dpsdce
I am fixing some old bash scripts I often see
我正在修复一些我经常看到的旧 bash 脚本
if [[ -n $VARIABLE ]]; then
syntax I tried to google it but could find why "-n" is used for, following is what I know
语法我试图谷歌它但可以找到为什么使用“-n”,以下是我所知道的
Comparisons:
-eq equal to
-ne not equal to
-lt less than
-le less than or equal to
-gt greater than
-ge greater than or equal to
File Operations:
文件操作:
-s file exists and is not empty
-f file exists and is not a directory
-d directory exists
-x file is executable
-w file is writable
-r file is readable
would anyone let me know what -n do ?
有没有人让我知道 -n 做什么?
回答by devnull
help test
would tell you:
help test
会告诉你:
String operators:
....
-n STRING
STRING True if string is not empty.
回答by Radu R?deanu
If $VARIABLE
is a string, then [ -n $VARIABLE ]
is true if the length of $VARIABLE
is non-zero.
如果$VARIABLE
是字符串,则[ -n $VARIABLE ]
如果 的长度$VARIABLE
不为零,则为真。
Also, [ -n $VARIABLE ]
is equivalent with: [ $VARIABLE ]
, when and only when $VARIABLE
is a string.
此外,[ -n $VARIABLE ]
等价于: [ $VARIABLE ]
, when and only when$VARIABLE
是一个字符串。
More about: Introduction to if
更多关于:if 简介
回答by David W.
The various teststhat [[ ... ]]
and [ ... ]
use in if
and while
loops are from the Unix test
command itself. An easy way to see what these various tests are is to look the testmanpage.
各种测试是[[ ... ]]
与[ ... ]
使用if
和while
循环是从Unix的test
命令本身。查看这些不同测试的一种简单方法是查看测试联机帮助页。
In Unix, the /bin/[
command is actually a hard link to the /bin/test
command. In early Unix systems, you would write this:
在 Unix 中,/bin/[
命令实际上是命令的硬链接/bin/test
。在早期的 Unix 系统中,你会这样写:
if test -n $parameter
then
echo "Parameter has a value"
fi
or
或者
if test $foo = $bar
then
echo "Foo and Bar are equal"
fi
The /bin/[
was created, so you could do this:
在/bin/[
被创造了,所以你可以这样做:
if [ -n $parameter ]
then
echo "Parameter has a value"
fi
and this
和这个
if [ $foo = $bar ]
then
echo "Foo and Bar are equal"
fi
This explains why the funny syntax and why you need a space between the square brackets and the parameters inside.
这解释了为什么有趣的语法以及为什么在方括号和里面的参数之间需要一个空格。
The [[ ... ]]
is actually a Korn shellism... I mean a POSIX shellism that BASH has takenborrowed. It was introduced to allow pattern matching tests ([[ $foo == bar* ]]
) and is internal to the shell, so its less sensitive to shell command line expansion issues. For example:
该[[ ... ]]
实际上是一个光辉shellism......我的意思是POSIX shellism的bash采取借用。它被引入以允许模式匹配测试 ( [[ $foo == bar* ]]
) 并且是 shell 内部的,因此它对 shell 命令行扩展问题不太敏感。例如:
if [ $foo = $bar ]
will fail if either $foo
or $bar
is not set while:
如果$foo
或$bar
未设置,则将失败:
if [[ $foo = $bar ]]
will work even if one of those two variables aren't set.
即使未设置这两个变量之一也将起作用。
The [[ ... ]]
syntax takes all of the same testing parameters that [ ... ]
does and is now preferred.
该[[ ... ]]
语法通吃相同的测试参数的[ ... ]
不和,现在首选。