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

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

如果在 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

我认为错误出在 EOJ 中。

答案1

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

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.

相关内容