Solved

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

Posted on 2011-02-22
18
1,166 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
ID: 34954384
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
ID: 34954418
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
ID: 34958989
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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

Author Comment

by:bowemc
ID: 34968645
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
ID: 34968980
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
ID: 34969162
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
ID: 34969185
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
ID: 34969230
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
ID: 34969282
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
 

Author Comment

by:bowemc
ID: 34969401
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
ID: 34969429
try to change

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

to

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

Author Comment

by:bowemc
ID: 34970381
SAME

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

Expert Comment

by:omarfarid
ID: 34970424
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
ID: 34972303
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
ID: 34973168
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
ID: 34977847
i've tried dos2unix
0
 
LVL 19

Expert Comment

by:simon3270
ID: 34978301
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
ID: 34978587
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
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…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

831 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