Bash脚本:语法错误:文件意外结束 [英] Bash script: syntax error: unexpected end of file

查看:65
本文介绍了Bash脚本:语法错误:文件意外结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下文件,并且该文件上有chmod a + x.当我尝试运行它时,出现第75行:语法错误:文件意外结束.我的脚本有什么错误?我需要做些什么来解决它?

I have the following file and I have chmod a+x on the file. When I try to run it, I get a line 75: syntax error: unexpected end of file. What is the error with my script? What do I need to do to fix it?

#!/bin/sh
# log directory for ascp transfer
logdirectory=/tmp/log20079
# log for this script
baselog=$logdirectory/sent4files.log
#directory of where the files to be transferred are located
basedirectory=/tmp/test20079/

#remote host data
REMOTE_DIR=/Upload
REMOTE_HOST=xxx
REMOTE_USER=xxx
# extensions of file
FEATURE_EXT=feature
KEYART_EXT=keyart
TRAILER_EXT=trailer
METADATA_EXT=metadata

# files to be excluded, must match exclude_file_suffix
COMPLETE_EXT=complete
SENT_EXT=sent

# file to send on completion
FILE_ON_COMPLETE="$basedirectory"delivery.complete

if [ "$TYPE" == "File" ]; then
   if [ "$STARTSTOP" == "Stop" ]; then
     if [ "$STATE" == "success" ]; then
        filename=$(basename "$FILE")
        extension="${filename##*.}"

# check the extension here, and create files ending in .sent as a flag

        case "$extension" in

        "$FEATURE_EXT")
           cat > "$basedirectory$FEATURE_EXT.$SENT_EXT" << 'EOF'
           EOF
           echo "Feature sent" >> $baselog
            ;;
        "$KEYART_EXT")
           cat > "$basedirectory$KEYART_EXT.$SENT_EXT" << 'EOF'
           EOF
           echo "Keyart sent" >> $baselog
            ;;
        "$TRAILER_EXT")
           cat > "$basedirectory$TRAILER_EXT.$SENT_EXT" << 'EOF'
           EOF
           echo "Trailer sent" >> $baselog
            ;;
        "$METADATA_EXT")
           cat > "$basedirectory$METADATA_EXT.$SENT_EXT" << 'EOF'
           EOF
           echo "Metadata sent" >> $baselog
           ;;
        esac


# check that all four files exists
        if [ -e "$basedirectory$FEATURE_EXT.$SENT_EXT" ] && [ -e "$basedirectory$KEYART_EXT.$SENT_EXT" ] && [ -e "$basedirectory$TRAILER_EXT.$SENT_EXT" ] && [ -e "$basedirectory$METADATA_EXT.$SENT_EXT" ]; then
          echo "All files sent" >> $baselog
          echo>$FILE_ON_COMPLETE
#         $FILE_ON_COMPLETE "$REMOTE_USER@$REMOTE_HOST:$REMOTE_DIR"
          rm "$basedirectory$FEATURE_EXT.$SENT_EXT"
          rm "$basedirectory$KEYART_EXT.$SENT_EXT"
          rm "$basedirectory$TRAILER_EXT.$SENT_EXT"
          rm "$basedirectory$METADATA_EXT.$SENT_EXT"
          rm $FILE_ON_COMPLETE
        fi

     fi
   fi
fi

推荐答案

这里的文档是棘手的野兽.如果您使用'EOF'恰好是 结束线,那么前面没有空格.

Heredocs are tricky beasts to get right. If you use 'EOF' that's exactly what the closing line needs to be, with no whitespace at the front like you have.

或者,您可以使用<<-变体,该变体从heredoc 的结束行中剥离所有前导制表符,如下成绩单(其中< tab> TAB 字符):

Alternatively, you can use the <<- variant which strips off all leading tab characters from the lines in the heredoc and the closing line as per the following transcript (where <tab> is the TAB character):

pax> cat <<-'eof'
...> 1
...< 2
...> <tab>eof
...> 4
...> eof

1
2
<tab>eof
4

pax> cat <<-'eof'
...> 1
...> 2
...> <tab>eof

1
2

使用<<-变体可以使文件更整洁,但是如果要保留开头的标签,那当然不好.在 bash 联机帮助页中:

Using the <<- variant allows for neater files, thoug it's no good if you want to preserve leading tabs of course. From the bash manpage:

如果重定向运算符是<<-,则所有前导制表符从输入行和包含定界符的行中除去.这样就可以自然地缩进shell脚本中的here-document.

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.

当然,如果您只是想将这些文件用作标记文件,则使用带有Heredoc的方法比 cat 更好.只需使用:

Of course, if you're just wanting to use those files as flag files, there's a better way than cat with a heredoc. Just use:

touch "$basedirectory$FEATURE_EXT.$SENT_EXT"

这将创建文件(如果文件不存在),并更新文件的修改时间,就像 cat 一样,但不会弄乱heredocs.它不会清空文件,但是,如果出于某些原因需要该文件:

This will create the file if it doesn't exist and update the modification time if it does, just like the cat but without messing about with heredocs. It won't empty the file but, if you need that for some reason:

rm -f "$basedirectory$FEATURE_EXT.$SENT_EXT"
touch "$basedirectory$FEATURE_EXT.$SENT_EXT"

可以解决问题.

但是,由于Heredoc实际上确实输出了一个空行(一个 \ n 字符),因此您可以选择:

However, since the heredoc does actually output a single empty line (one \n character), you can opt for:

echo >"$basedirectory$FEATURE_EXT.$SENT_EXT"

相反.

这篇关于Bash脚本:语法错误:文件意外结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆