SFTP error in UNIX

I have this code snippet:

sftp $USERNAME@$REMOTE_SERVER -omode=ascii >> $LOG_FILEPATH/$LOG_FILENAME  2>&1                                    
ls /+showall                                                                                                    
ls /+ mode=text,recfm=fb,lrecl=80,space=trk.8.1,release                                                            
##quote site file=jes
pwd                                                                                                                
lcd $LOCAL_DIRECTORY
lpwd                                                                                                            
$DIRECTION $LOCAL_FILENAME $REMOTE_FILENAME                                                                        
ls $REMOTE_FILENAME                                                                                                
cd //$REMOTE_DIRECTORY                                                                                            
ls /+mode=text,jesjobwait                                                                                        
lcd $BIN_PATH                                                                                                    
lpwd                                                                                                            
$DIRECTION $LOCAL_JOBNAME $REMOTE_JOBNAME                                                                        
ls $REMOTE_JOBNAME                                                                                                
get /+/error.log                                                                                                 
quit

and i get the following error when i execute this program:

syntax error at line 157: `end of file' unexpected

Please advise.

Thanks,
Jess

I do not see where you say to sftp its in batch mode for a start...
How do you expect us to tell you what is wrong giving us only partial code, when error mentions line 157?

Can you reproduce that code snippet with line numbers, or at least point out where line 157 is, please?

Moderator comments were removed during original forum migration.

1 Like
USERNAME="$1"
export USERNAME
REMOTE_SERVER="$2"
export REMOTE_SERVER
LOCAL_DIRECTORY="$3"
export LOCAL_DIRECTORY
REMOTE_DIRECTORY="$4"
export REMOTE_DIRECTORY
DIRECTION="$5"
export DIRECTION
LOCAL_FILENAME="$6"
export LOCAL_FILENAME
REMOTE_FILENAME="$7"
export REMOTE_FILENAME
LOGFILE_PATH="$8"
export LOGFILE_PATH
LOG_FILENAME="$9"
export LOG_FILENAME
BIN_PATH="$10"
export BIN_PATH
LOCAL_JOBNAME="$11"
export LOCAL_JOBNAME
REMOTE_JOBNAME="$12"
export REMOTE_JOBNAME

##Validating if all inputs were entered
echo "Entering the program `date`" 																				| tee -a $LOG_FILEPATH/$LOG_FILENAME
if [ $# -ne 12 ]
then
echo "One or more inputs to this program were not entered." 													| tee -a $LOG_FILEPATH/$LOG_FILENAME
echo "Please verify the Appworx module" 																		| tee -a $LOG_FILEPATH/$LOG_FILENAME
echo "Aborting the program on `date`"																			| tee -a $LOG_FILEPATH/$LOG_FILENAME
exit 1
else
echo "All inputs to this program have been entered" 															| tee -a $LOG_FILEPATH/$LOG_FILENAME
fi

echo "Connecting to the BLUE Mainframe with USER:$USERNAME" 													| tee -a $LOG_FILEPATH/$LOG_FILENAME
echo "Remote host is: $REMOTE_SERVER"                                                                           | tee -a $LOG_FILEPATH/$LOG_FILENAME
sftp $USERNAME@$REMOTE_SERVER -omode=ascii >> $LOG_FILEPATH/$LOG_FILENAME  2>&1                        	        
ls /+showall																									
ls /+ mode=text,recfm=fb,lrecl=80,space=trk.8.1,release															
##quote site file=jes
pwd																												
lcd $LOCAL_DIRECTORY
lpwd																											
$DIRECTION $LOCAL_FILENAME $REMOTE_FILENAME																		
ls $REMOTE_FILENAME																								
cd //$REMOTE_DIRECTORY																							
ls /+mode=text,jesjobwait																						
lcd $BIN_PATH																									
lpwd																											
$DIRECTION $LOCAL_JOBNAME $REMOTE_JOBNAME																		
ls $REMOTE_JOBNAME																								
get /+/error.log 																								
quit

echo "SFTP connection successfully exited"  																	| tee -a $LOG_FILEPATH/$LOG_FILENAME
echo "Checking the log file for errors"																			| tee -a $LOG_FILEPATH/$LOG_FILENAME

cd $LOG_FILEPATH
sftp_check=`grep -i "Aborting the program" $LOG_FILENAME | wc -l`
if [ "$sftp_check" -gt 0 ]
then
        echo '\n'
        echo "##################################"
        echo  ERROR: Please enter the input parameters correctly.
        echo  'SFTP to Blue Server failed'
        echo "##################################"
        echo `date`
        exit 1
fi

sftp_check=`grep -i "Couldn't stat remote file" $LOG_FILENAME | wc -l`
if [ "$sftp_check" -gt 0 ]
then
        echo '\n'
        echo "##################################"
        echo  ERROR: SFTP Transfer Not Completed
        echo  'SFTP to Blue Server failed'
        echo "##################################"
        echo `date`
        exit 1
fi

sftp_check=`grep -i "No such file or directory" $LOG_FILENAME | wc -l`
if [ "$sftp_check" -gt 0 ]
then
        echo '\n'
        echo "##################################"
        echo  ERROR: SFTP Transfer Not Completed
        echo  'SFTP to Blue Server failed'
        echo "##################################"
        echo `date`
        exit 1
fi

echo "Script Completed"
echo"Exiting the program on `date`"

That are 100 lines only, so still no syntax error at line 157 possible. To me, that msg sounds like an unpaired single/double quote somewhere in the script.

What about the second sftp_check ?

there's no here-doc for the sftp block.
How do you expect this to work?
What are all these '| tee ' commands on the right?

Brrrr......

I have a modification log and comments which i removed, hence 157 lines.

---------- Post updated at 10:21 PM ---------- Previous update was at 10:18 PM ----------

The tee commands append the output of the commands(outside) sftp to the log file.
What's a here-doc.
The SFTP portion works just fine.. but when i parameterize it doesnt work..

Forgive me if I seem to out of my mind - Its end of day and was a had week for me...
my 2 cents:
On top of RudiC remarks above
I dont quite understand this line ( the use??)

echo "Remote host is: $REMOTE_SERVER"                                                                           | tee -a $LOG_FILEPATH/$LOG_FILENAME

the tee that is which can lead to some strange issues...
and now:

sftp $USERNAME@$REMOTE_SERVER -omode=ascii >> $LOG_FILEPATH/$LOG_FILENAME  2>&1                        	        
ls /+showall																									
ls /+ mode=text,recfm=fb,lrecl=80,space=trk.8.1,release															
##quote site file=jes
pwd																												
lcd $LOCAL_DIRECTORY
lpwd																											
$DIRECTION $LOCAL_FILENAME $REMOTE_FILENAME																		
ls $REMOTE_FILENAME																								
cd //$REMOTE_DIRECTORY																							
ls /+mode=text,jesjobwait																						
lcd $BIN_PATH																									
lpwd																											
$DIRECTION $LOCAL_JOBNAME $REMOTE_JOBNAME																		
ls $REMOTE_JOBNAME																								
get /+/error.log 																								
quit
.
.
.

Please forgive me if Im worng but I have a very poor eyesight this week... Typos as well...
This looks like a heredoc syntax , and if it is the case then there is something wrong in the syntax as I see not the << redirection though
I suppose the quit must be in relation or I am missing even more in trying to understand the given code

So?

1 Like

Thanks vgersh99, you were faster then me and I did not see the other tees haha

For our new member

This explains what a here-doc is:

Here Documents

My understanding is the sftp works fine when in interactive mode and you transcribed down what you want your batch script to do

most sftp have a -b option for batch mode too...

1 Like