• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1295
  • Last Modified:

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

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
bowemc
Asked:
bowemc
  • 7
  • 7
  • 4
1 Solution
 
simon3270Commented:
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
 
simon3270Commented:
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
 
omarfaridCommented:
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
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
bowemcAuthor Commented:
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
 
omarfaridCommented:
you just posted the error, can you post fiew lines before and after that? what is line 267 in your script?
0
 
bowemcAuthor Commented:
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
 
omarfaridCommented:
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
 
bowemcAuthor Commented:
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
 
omarfaridCommented:
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
 
bowemcAuthor Commented:
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
 
omarfaridCommented:
try to change

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

to

if [ "$DAY" = "Sat" -o "$DAY" = "Sun" ] -a [ "$ARG1" = "" ]
0
 
bowemcAuthor Commented:
SAME

./edge_kpi.startup: line 267: syntax error: unexpected end of file
0
 
omarfaridCommented:
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
 
bowemcAuthor Commented:
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
 
omarfaridCommented:
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
 
bowemcAuthor Commented:
i've tried dos2unix
0
 
simon3270Commented:
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
 
simon3270Commented:
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

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 7
  • 7
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now