Solved

FTP scripting error - line 267: syntax error: unexpected end of file

Posted on 2011-02-22
18
1,146 Views
Last Modified: 2013-12-26
Hi,

The below lines are throwing the eblow error in my shell script on Linux. It worked previously which is what puzzles me!

 ftp -v -n $CONQUEST_FTP_HOST >> $LOG_PATH 2>> $LOG_PATH <<-END_SCRIPT
                user $CONQUEST_FTP_USER $CONQUEST_FTP_PASS
                lcd $LOG_KPI_DIR
                put $CSV_FILENAME $FTP_CSV_NAME
                quit
                END_SCRIPT


ERROR: line 267: syntax error: unexpected end of file
0
Comment
Question by:bowemc
  • 7
  • 7
  • 4
18 Comments
 
LVL 19

Expert Comment

by:simon3270
Comment Utility
Has the file been reformatted?

The END_SCRIPT lines needs to start at the left-hand side of the file, and not be indented.
0
 
LVL 19

Expert Comment

by:simon3270
Comment Utility
Ah, didn't spot the "-" after the << (I think my font is too small!).

It *should* work, unless you are using an old shell whcih doesn't support <<-.
0
 
LVL 40

Expert Comment

by:omarfarid
Comment Utility
how you run the script?

can you run the script with

bash -x scriptname

and post the output?

It could be than one of the variables is not set or that you face problems with one of the ftp commands
0
 

Author Comment

by:bowemc
Comment Utility
I've replaced "-" with a " " blank caharacter and the same effect

omarfarid, below is the output of your command. It's the same as when I run ./edge_kpi.startup

bash-3.00$ bash -x edge_kpi.startup
edge_kpi.startup: line 267: syntax error: unexpected end of file
0
 
LVL 40

Expert Comment

by:omarfarid
Comment Utility
you just posted the error, can you post fiew lines before and after that? what is line 267 in your script?
0
 

Author Comment

by:bowemc
Comment Utility
267 is the last line of the script

The last few line are:
#########################
####  MAIN (METHOD): ####

PWD=`pwd`
ARG1=$1
DAY=`date +%a`

if [ -f $PWD/edge_kpi.properties ] ; then
   . $PWD/edge_kpi.properties
else
   echo "${0}: Cannot load $PWD/edge_kpi.properties . Quitting." >&2
   exit 1
fi

LOG_KPI_DIR=$PWD
LOG_FILE=edgeKPI.$(date +"%Y%m%d").log
LOG_PATH=$LOG_KPI_DIR/$LOG_FILE

if [ "$DAY" = "Sat" -o "$DAY" = "Sun" ] && [ "$ARG1" = "" ]; then
        echo "Weekend: Script will not execute as an at-job."
else
        scriptLauncher
fi
emailLog
cd $PWD  # cd to the dir that the user started with, just for elegance

Open in new window

0
 
LVL 40

Expert Comment

by:omarfarid
Comment Utility
what I meant is that to post few lines before the error line from the output of

bash -x edge_kpi.startup

and the line 267
0
 

Author Comment

by:bowemc
Comment Utility
bash-3.00$
bash-3.00$
bash-3.00$
bash-3.00$ clear
bash-3.00$ ./edge_kpi.startup
./edge_kpi.startup: line 267: syntax error: unexpected end of file
bash-3.00$
bash-3.00$
bash-3.00$
bash-3.00$ bash -x edge_kpi.startup
edge_kpi.startup: line 267: syntax error: unexpected end of file
bash-3.00$
bash-3.00$
bash-3.00$



and line 267 is


cd $PWD  # cd to the dir that the user started with, just for elegance
0
 
LVL 40

Expert Comment

by:omarfarid
Comment Utility
it looks like that your script is not complete and it complains before even executing any line. can you post the full script?

what the file $PWD/edge_kpi.properties contains?
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:bowemc
Comment Utility
bash-3.00$ more edge_kpi.properties
##KPI Properties
export EDGE_EMAIL="xxx@xxx.com"
export CONQUEST_FTP_HOST=xxx.xx.xxx.com
export CONQUEST_FTP_USER=xxx
export CONQUEST_FTP_PASS=xxx@xxx.com


#SYSTEM ID
export CONQUEST_SYSTEM_ID=12



#!/bin/bash

function scriptLauncher()
{
        checkVars
        if [ "$STATUS" = "Unsuccessful" ]; then

        else
                setVars
                setupLogging
                echoConfig
                STATUS=Successful

                grepLogs
                genCSV
                sendFile
                removeFile
        fi

        echo $STATUS
}


function checkVars()
{

        if [ -z "$CONQUEST_FTP_HOST" ]; then
                STATUS=Unsuccessful
        fi

        if [ -z "$CONQUEST_FTP_USER" ]; then
                STATUS=Unsuccessful
        fi

        if [ -z "$CONQUEST_FTP_PASS" ]; then
                STATUS=Unsuccessful
        fi

        if [ -z "$EDGE_EMAIL" ]; then
                STATUS=Unsuccessful
        fi

}


function setVars()
{
        # ****** MISC VARS ******
        DATE=`date +"%Y%m%d"`

        # Check if date override was passed as argument
        if [ "$ARG1" != "" ]; then
                DATE=$(echo $ARG1 | sed 's/-//g')
                LOG_SUFFIX=".$ARG1"
        fi

        CSV_FILENAME=edgeKPI.$DATE.csv
        FTP_CSV_NAME=$CSV_FILENAME
        CSV_FILE=$LOG_KPI_DIR/$CSV_FILENAME

        # ****** LOG_ VARS (edge) ******
        LOG_EDGE=$LOG_KPI_DIR/../logs/edge.log$LOG_SUFFIX
        #************************
}


function setupLogging()
{
        if ! [ -d $LOG_KPI_DIR ]
        then
                mkdir $LOG_KPI_DIR
        fi

        if ! [ -f $LOG_PATH ]
        then
                touch $LOG_PATH
        fi

        # Find, then remove log files older than 30 days

        FOUND=`find $LOG_KPI_DIR/edgeKPI.*.log -mtime +30 -print | wc -l`

        if [ $FOUND -gt 0 ]
        then
               find $LOG_KPI_DIR/edgeKPI.*.log -mtime +30 -print | xargs rm -f
        fi
}


function echoConfig()
{
        if [ "$ARG1" != "" ]; then
                log "INFO:\t++Date override received. Business date will be set to $DATE.++"
        fi

        log "INFO:\t--Log file for edge set to: $LOG_EDGE --"
}



function log()
{
        echo -e "$(date +%H:%M:%S) $*" >> $LOG_PATH
}


function grepLogs()
{

        if  [ -f $LOG_EDGE ]; then
                EDGE_START_TIME=`grep "Startup took" ../logs/edge.log  | awk '{print $9}'`

        fi

        echoResult
}



function echoResult()
{
        log  "INFO:\t**Result from the 'grep' statements:
                        EdgE Startup: $EDGE_START_TIME"
}


function genCSV()
{
        log "INFO:\t**Currently: generating $CSV_FILE"
        if [ -f $CSV_FILE ]
        then
                log "INFO:\t**Deleting existing $CSV_FILE file."
                rm $CSV_FILE
        fi

        touch $CSV_FILE

        echo "id_system=$CONQUEST_SYSTEM_ID,id_param=EDGE_START_TIME,id_value=$EDGE_START_TIME,id_time_end=$DATE" >> $CSV_FILE

}



function sendFile()
{
        log "INFO:\t**Currently: FTP'ing $CSV_FILE"

        if [ -f $CSV_FILE ]     #CHECK IF the file exists then FTP
        then
                ftp -v -n $CONQUEST_FTP_HOST >> $LOG_PATH 2>> $LOG_PATH <<-END_SCRIPT
                user $CONQUEST_FTP_USER $CONQUEST_FTP_PASS
                lcd $LOG_KPI_DIR
                put $CSV_FILENAME $FTP_CSV_NAME
                quit
                END_SCRIPT

                if ! cat $LOG_PATH | tail -10 | grep "226 Transfer complete."
                then
                        log "ERROR:\tFile transfer did not complete successfully."
                        STATUS=Unsuccessful
                fi
        else
                log "ERROR: Could not transfer file - '$CSV_FILE' not found."
                STATUS=Unsuccessful
        fi
        log "INFO:\t**Currently: FTP complete"
}



function removeFile()
{
        #rm $CSV_FILE
        find -name "*.csv" -mtime +31 -exec rm {} \;
}



function emailLog()
{
        if [ "$STATUS" = "Unsuccessful" ]; then
                cd $LOG_KPI_DIR
                uuencode $LOG_FILE $LOG_FILE | mail -s "...EdgE KPI Scipt Log File: $STATUS..." "$EDGE_EMAIL"
        fi
}


#########################
####  MAIN (METHOD): ####

PWD=`pwd`
ARG1=$1
DAY=`date +%a`

if [ -f $PWD/edge_kpi.properties ] ; then
   . $PWD/edge_kpi.properties
else
   echo "${0}: Cannot load $PWD/edge_kpi.properties . Quitting." >&2
   exit 1
fi

LOG_KPI_DIR=$PWD
LOG_FILE=edgeKPI.$(date +"%Y%m%d").log
LOG_PATH=$LOG_KPI_DIR/$LOG_FILE

if [ "$DAY" = "Sat" -o "$DAY" = "Sun" ] && [ "$ARG1" = "" ]; then
        echo "Weekend: Script will not execute as an at-job."
else
        scriptLauncher
fi
emailLog
cd $PWD  # cd to the dir that the user started with, just for elegance

Open in new window

0
 
LVL 40

Expert Comment

by:omarfarid
Comment Utility
try to change

if [ "$DAY" = "Sat" -o "$DAY" = "Sun" ] && [ "$ARG1" = "" ]

to

if [ "$DAY" = "Sat" -o "$DAY" = "Sun" ] -a [ "$ARG1" = "" ]
0
 

Author Comment

by:bowemc
Comment Utility
SAME

./edge_kpi.startup: line 267: syntax error: unexpected end of file
0
 
LVL 40

Expert Comment

by:omarfarid
Comment Utility
the problem is with the if statement. can you try removing the other part?

if [ "$DAY" = "Sat" -o "$DAY" = "Sun" ] ; then

I am not sure if this will work:

if [ ("$DAY" = "Sat" -o "$DAY" = "Sun")  -a  "$ARG1" = "" ] ; then
      
0
 

Author Comment

by:bowemc
Comment Utility
same error

how do u know it's this line?

I've commented out this line and the error still happens.

The error goes away when i comment out the ftp part
0
 
LVL 40

Expert Comment

by:omarfarid
Comment Utility
how the script is edited? do you edit it on windows and upload to unix? it could be that you need to edit the script on unix using vi or you may i=upload it as text (if you use ftp) or you use dostunix tool to convert to unix text
0
 

Author Comment

by:bowemc
Comment Utility
i've tried dos2unix
0
 
LVL 19

Expert Comment

by:simon3270
Comment Utility
I was (sort of) right in the first place - it is the END_SCRIPT line (line 155 in ID:34969401 above).

The "<<-" removes tabs at the start of the line - you have spaces (as far as I can tell).

So either change the spaces in front of "END_SCRIPT" to tabs, or just move that text to the start of the line.
0
 
LVL 19

Accepted Solution

by:
simon3270 earned 500 total points
Comment Utility
By the way, the script as presented above also failed earlier on.  Your first function is shown as:

function scriptLauncher()
{
        checkVars
        if [ "$STATUS" = "Unsuccessful" ]; then

        else
                setVars
                setupLogging
                echoConfig
                STATUS=Successful

Open in new window


Having no code between "then" and "else" (or between "else" and "fi", or "do" and "done") is not valid.  (I assume you've removed some private code here).  If you do want nothing to happen (e.g. if it is hard or messy to write a negative test, and you only want something to happen if a positive test fails), then just put a line starting with a colon on the line netween the then and else, as:
        if [ "$STATUS" = "Unsuccessful" ]; then
                : do nothing line
        else
                setVars
                setupLogging

Open in new window

You can't just insert a comment here - it needs to be a command.  ":" is the "do nothing" command - it does no work and doesn't produce any output, but is a valid command.  You have to be a bit careful about what follows the colon - unlike for a comment, the shell does interpret the rest of the line, including any file redirections, variable assignments.  and quotes.  For example, the following:
                : check file ${var:=is_null} > new_file

Open in new window

will set the value of $var1 if it was null, and will make new_file an empty file, while
                # check file ${var:=is_null} > new_file

Open in new window

would do neither of those (but would also not be a valid command line to go between "then" and "else").
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Utilizing an array to gracefully append to a list of EmailAddresses
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now