gnuwin32 程序:[.exe 的作用是什么?

发布于 2024-07-09 06:24:16 字数 469 浏览 8 评论 0 原文

查看 gnuwin32/bin 目录,有一个名为 [.exe 的奇怪程序文件,

我在文档、gnuwin32.sourceforge.net 或 google 搜索中找不到它,所以我运行它并得到:

$ [
[: missing `]'
$

所以我给它 ] 作为参数并得到

$ [ ]

$

它没有抱怨,所以我认为它处于正确的轨道上。 我

$ [ hello ]

再次尝试:没有抱怨。 所以我尝试了一个算术表达式:

$ [ 1 + 1 ]
[: +: binary operator expected
$

我尝试了一堆不同的组合,包括前缀和; 后缀表示法,但似乎没有任何作用。 这个东西有什么作用?

Looking in the gnuwin32/bin directory, there is an odd-looking program file named [.exe

I couldn't find it in the documentation, gnuwin32.sourceforge.net or in a google search, so I ran it and got:

$ [
[: missing `]'
$

so I gave it ] as a parameter and got

$ [ ]

$

It didn't complain, so I assumed it was on the right track. I tried:

$ [ hello ]

again, no complaints. so I tried an arithmetic expression:

$ [ 1 + 1 ]
[: +: binary operator expected
$

I tried a bunch of different combinations, including prefix & postfix notation but nothing seemed to work. What does this thing do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烟酉 2024-07-16 06:24:16
test a

==

[ a ]

这只是糖

编辑:澄清一下,这是条件语法,例如 [“a”=“a”]

test a

==

[ a ]

It's just sugar

Edit: To clarify, that's the conditional syntax, e.g. [ "a" = "a" ]

情话难免假 2024-07-16 06:24:16

它用于评估条件表达式。
它相当于(可能是符号链接?)test 可执行文件。
手册页位于此处

您可能会在很多 bash 脚本中看到这一点:

if [ "$LOGNAME" = "scott" ]
then
    echo "Logged in as Scott"
else
     echo "incorrect user"
fi

有趣的是, [ 不是 bash 语言的一部分,它实际上是一个可执行文件,其返回代码由“如果”。 这就是为什么 [ 及其第一个参数后面的空格是强制性的 - 如果省略它,脚本将尝试执行 [ “$LOGNAME” 并失败。

您不能用它进行算术运算 - 使用 expr (请参阅 此处)。
但是,您可以测试各种文件属性(是否存在?它是什么类型?等)以及对字符串和数字使用比较运算符。

It's used to evaluate conditional expressions.
It is equivalent to (possibly a symlink to?) the test executable.
The manpage is here.

You may see this in a lot of bash scripts:

if [ "$LOGNAME" = "scott" ]
then
    echo "Logged in as Scott"
else
     echo "incorrect user"
fi

The funny thing is, the [ is not part of the bash language, it's actually an executable whose return code is used by the 'IF'. This is the reason why the space after the [ and its first argument is mandatory - if it would be omitted, the script would try to execute ["$LOGNAME" and fail.

You can't do arithmetical operations with it - use expr for that (see here).
However, you can test for a wide range of file properties (does it exist? what type is it? etc) as well as use comparison operators on strings and numbers.

流殇 2024-07-16 06:24:16

另一个答案已经提到它与 test 相同。 在 bash 上,它也是一个内置函数,因此您可以使用 help 内置函数 (help test) 获取它的帮助。

Another answer already mentioned it is the same as test. On bash, it is also a builtin, so you can get the help for it with the help builtin (help test).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文