小编Ste*_*ker的帖子

当找不到可执行文件时,“which”的任何实现是否输出“no”?

我正在阅读为 Bourne shell 编写的 Maven 包装器的源代码。我遇到过这些行:

if [ -z "$JAVA_HOME" ]; then
    javaExecutable="$(which javac)"
    if [ -n "$javaExecutable" ] && ! [ "$(expr "$javaExecutable" : '\([^ ]*\)')" = "no" ]; then
# snip
Run Code Online (Sandbox Code Playgroud)

expr当与arg1and一起使用时arg2, a 与正则表达式:匹配。通常,结果是匹配字符的数量,例如:arg1arg2

$ expr foobar : foo
3
Run Code Online (Sandbox Code Playgroud)

但是,当使用捕获括号 (\(\)) 时,它返回第一个捕获括号的内容:

$ expr foobar : '\(foo\)'
foo
Run Code Online (Sandbox Code Playgroud)

到目前为止,一切都很好。

如果我在我的机器上评估上面引用的源中的表达式,我会得到:

$ javaExecutable=$(which javac)
$ expr "$javaExecutable" : '\([^ ]*\)'
/usr/bin/javac
Run Code Online (Sandbox Code Playgroud)

对于不存在的可执行文件:

$ …
Run Code Online (Sandbox Code Playgroud)

shell history which bourne-shell

4
推荐指数
2
解决办法
949
查看次数

标签 统计

bourne-shell ×1

history ×1

shell ×1

which ×1