Bash 语法错误:“[[: 未找到”

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

Bash syntax error: "[[: not found"

bash

提问by Justin

I'm working with a bash script that is currently working on a server (RHEL4). I'm developing on my laptop with Ubuntu 10.04, but I don't think the platform is causing the problem.

我正在使用当前在服务器 (RHEL4) 上运行的 bash 脚本。我正在使用 Ubuntu 10.04 的笔记本电脑进行开发,但我认为不是该平台导致了问题。

Here's what's happening: I have a skeleton script that calls another script that does most of the work. However, it makes calls to getConfig.sh a lot. getConfig.sh basically just parses some command line argument (using getopts) and calls a Java program to parse some XML files. Anyways, getConfig.sh is throwing up lots of errors (but still seems to work).

下面是正在发生的事情:我有一个框架脚本,它调用另一个完成大部分工作的脚本。但是,它会大量调用 getConfig.sh。getConfig.sh 基本上只是解析一些命令行参数(使用 getopts)并调用 Java 程序来解析一些 XML 文件。无论如何, getConfig.sh 抛出了很多错误(但似乎仍然有效)。

Here's the message that I'm getting

这是我收到的消息

getconfig.sh: 89: [[: not found
getconfig.sh: 89: [[: not found
getconfig.sh: 94: [[: not found

getconfig.sh: 89: [[: 未找到
getconfig.sh: 89: [[: 未找到
getconfig.sh: 94: [[: 未找到

每次运行时我都会遇到这三个错误;但是,脚本完成并运行 Java 代码。

Here's the relavent code section

这是相关代码部分

parseOptions $*

if [[ "${debugMode}" == "true" ]] ; then
    DEBUG="-DDEBUG=true"
    echo "${JAVA_HOME}/bin/java ${DEBUG} -Djava.endorsed.dirs=${JAXP_HOME} -jar $(dirname 
#!/bin/bash
)/GetXPath.jar ${XML_File} ${XPath_Query}" fi

Line 89 is "parseOptions $* and line 94 is "fi"

第 89 行是“parseOptions $*”,第 94 行是“fi”

Thanks for the answers.

感谢您的回答。

回答by Paused until further notice.

If your script is executable and you are executing it like ./getconfig.sh, the first line of your script needs to be:

如果您的脚本是可执行的并且您正在执行它./getconfig.sh,则脚本的第一行必须是:

##代码##

Without that shebang line, your script will be interpreted by shwhich doesn't understand [[in ifstatements.

如果没有那个 shebang 行,您的脚本将被解释为shwhich[[if语句中不理解。

Otherwise, you should run your script like bash getconfig.sh, notsh getconfig.sh. Even if your default shell is bash, scripts run with shwill use a reduced set of bash's features, in order to be more compliant with the POSIX standard. [[is one of the features that is disabled.

否则,您应该像 一样运行脚本bash getconfig.sh而不是sh getconfig.sh. 即使您的默认 shell 是 bash,运行的脚本sh也会使用一组减少的 bash 功能,以便更符合 POSIX 标准。[[是禁用的功能之一。

回答by Gangadhar

If you are checking for equality, shouldn't the if be ?

如果您正在检查相等性,那么 if 不应该是吗?

if [[ "${debugMode}" = "true" ]]; then .... fi

if [[ "${debugMode}" = "true" ]]; then .... fi

回答by Daniel Kmak

Use:

用:

bash scriptname.sh

bash scriptname.sh

instead of:

代替:

sh scriptname.sh

sh scriptname.sh