Bash script: syntax error: unexpected end of file

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

#!/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 
+4
source share
2 answers

Heredocs are tricky beasts to get better. If you use 'EOF' , what exactly should be the closing line, with no spaces in front, like yours.

Alternatively, you can use the <<- option, which removes all leading tab characters from the lines in the heredoc and the closing line according to the following decoding (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 

Using the <<- option allows you to use more accurate files, but this is not useful if you want, of course, to save the main tabs. From the bash manpage:

If the redirection operator is <<- , then all leading tab characters are removed from the input lines and the line containing the delimiter. This allows here-documents in shell scripts to be indented in a natural way.

Of course, if you just want to use these files as flag files, it is better to use cat with heredoc. Just use:

 touch "$basedirectory$FEATURE_EXT.$SENT_EXT" 

This will create the file if it does not exist, and update the modification time if it is like cat , but without chaos with heredocs. It will not empty the file, but if you need it for some reason:

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

will do the trick.

However, since heredoc does display one blank line (single character \n ), you can choose to:

 echo >"$basedirectory$FEATURE_EXT.$SENT_EXT" 

instead.

+6
source

There can be no spaces before them at the end of your heredocs EOF . Remove these spaces.

+1
source

Source: https://habr.com/ru/post/1485725/


All Articles