bash脚本中的“意外的文件结尾”

lud*_*egu 5 bash shell-script here-document

如果在 bash 脚本中,我有这个:

if [ $ACTION = deploy ]; then
    ${JAVA_HOME}/bin/java ${JVM_ARGS} weblogic.WLST << EOJ
    connect('XXX','XXX','t3://XXX:8001')
    jndi();
    ls();
    disconnect();
    exit ();
    EOJ
else
    echo "XXX"
fi
Run Code Online (Sandbox Code Playgroud)

我认为错误出在 EOJ 中。

Pet*_*r.O 9

EOJ需要完全左对齐,即。没有前导空格,也没有尾随空格。此外,您可以/应该(取决于您的需要)将第一个写为<<'EOJ'.. 引号禁用一些可能发生的外壳扩展。

info bash

Here Documents 这种类型的重定向指示 shell 从当前源读取输入,直到看到仅包含分隔符(没有尾随空格)的行。然后将读取到该点的所有行用作命令的标准输入。

   The format of here-documents is:

          <<[-]word
                  here-document
          delimiter

   No  parameter expansion, command substitution, arithmetic expansion, or
   pathname expansion is performed on word.  If any characters in word are
   quoted,  the  delimiter is the result of quote removal on word, and the
   lines in the here-document are not expanded.  If word is unquoted,  all
   lines  of  the here-document are subjected to parameter expansion, com?
   mand substitution, and arithmetic expansion.  In the latter  case,  the
   character  sequence  \<newline> is ignored, and \ must be used to quote
   the characters \, $, and `.

   If the redirection operator is <<-, then all leading tab characters are
   stripped  from  input  lines  and  the line containing delimiter.  This
   allows here-documents within shell scripts to be indented in a  natural
   fashion.
Run Code Online (Sandbox Code Playgroud)