LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-28-2009, 12:28 PM   #1
jmsans
LQ Newbie
 
Registered: Dec 2009
Posts: 4

Rep: Reputation: 0
Shell script terminated after ftp transfer


Hi,
I made a shell script with the following piece of code to transfer some files via ftp:

Code:
echo 'before ftp'

      ftp -n $FtpHost << EOF
        quote USER $FtpUser
        quote PASS $FtpPasswd
        quote CWD $FtpDir
        put $FileNameLog
        put $FileNameSqlTarSum
        put $FileNameSqlTar
        put $FileNameWwwTarSum
        put $FileNameTar
        quit
      EOF

echo 'before rm'
        
      rm $FileNameWwwTar $FileNameWwwTarSum $FileNameSql $FileNameSqlTar $FileNameSqlTarSum $FileNameLog

echo 'after rm'
The problem is that the script ends after the "quit" command in FTP, so code after it won't run, like the remove of temporary files. If I remove this "quit" command, the shell understands I'm still parsing commands to FTP, although being after the EOF. I used this echo commands to check which parts of the script are running.

Do somebody have a clue of why is this happening?

Thank you,
Josep Maria
 
Old 12-28-2009, 01:05 PM   #2
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
It's not really that the commands afterwards aren't being run.. it's that the here-doc never actually ends. The EOF token cannot be indented.
 
Old 12-28-2009, 01:06 PM   #3
GooseYArd
Member
 
Registered: Jul 2009
Location: Reston, VA
Distribution: Slackware, Ubuntu, RHEL
Posts: 183

Rep: Reputation: 46
Quote:
Originally Posted by jmsans View Post
Hi,
I made a shell script with the following piece of code to transfer some files via ftp:

Code:
echo 'before ftp'

      ftp -n $FtpHost << EOF
        quote USER $FtpUser
        quote PASS $FtpPasswd
        quote CWD $FtpDir
        put $FileNameLog
        put $FileNameSqlTarSum
        put $FileNameSqlTar
        put $FileNameWwwTarSum
        put $FileNameTar
        quit
      EOF

echo 'before rm'
        
      rm $FileNameWwwTar $FileNameWwwTarSum $FileNameSql $FileNameSqlTar $FileNameSqlTarSum $FileNameLog

echo 'after rm'
The problem is that the script ends after the "quit" command in FTP, so code after it won't run, like the remove of temporary files. If I remove this "quit" command, the shell understands I'm still parsing commands to FTP, although being after the EOF. I used this echo commands to check which parts of the script are running.

Do somebody have a clue of why is this happening?

Thank you,
Josep Maria
I haven't got the manpage handy, but I assume the script exits because the ftp is returning a non-zero exit status. One way around that is to change the invokation of ftp to something like:

ftp <whatever args> || /bin/true

scripting the generic ftp client this way is a lot more effort than using an ftp client meant for batch work though- I would have a look at something like ncftpput, from the ncftp package. Way less headache.
 
Old 12-28-2009, 02:23 PM   #4
jmsans
LQ Newbie
 
Registered: Dec 2009
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
It's not really that the commands afterwards aren't being run.. it's that the here-doc never actually ends. The EOF token cannot be indented.
tuxdev, you're right, here-docs mustn't be indented. But problem persists after removing the space.

Quote:
I would have a look at something like ncftpput, from the ncftp package. Way less headache.
Sure it's less headache, tried ncftpput and now it works.
Thanks to both of you!
 
Old 12-28-2009, 02:56 PM   #5
vrmartin2
Member
 
Registered: Dec 2009
Location: NE Ohio
Distribution: Open SUSE
Posts: 43

Rep: Reputation: 19
The first thing I'd try is to remove the "quit" That may not help, but I'm unclear why you need it since your input ends anyway.
 
Old 12-28-2009, 03:19 PM   #6
jmsans
LQ Newbie
 
Registered: Dec 2009
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
If I remove this "quit" command, the shell understands I'm still parsing commands to FTP, although being after the EOF.
If you open an ftp session with the ftp command in a shell, you use the quit command to end the ftp session. I understand quit is needed in order to close the connection, and all posts I read with code on ftp transfers in scripts use it. What I don't understand is why the script terminates with this "quit" being inside the here-doc.
 
Old 12-29-2009, 01:27 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Indeed it should not terminate. My guess is that it still cannot interpret the EOF at the end of the here document, as previously suggested by tuxdev. Be sure the ftp part of your script loosk like:
Code:
ftp -n $FtpHost << EOF
  quote USER $FtpUser
  quote PASS $FtpPasswd
  quote CWD $FtpDir
  put $FileNameLog
  put $FileNameSqlTarSum
  put $FileNameSqlTar
  put $FileNameWwwTarSum
  put $FileNameTar
  quit
EOF
the closing EOF must be at the very beginning of the line (no spaces before) unless you use another form of the "here document" syntax as explained in the Advanced Bash User Guide (but this is another story for now).

Moreover, take in mind that you can quickly debug your script using the -x option of bash. This will put on the standard output every command executed by the shell after expansions and substitutions being performed. In this way you can check what the shell actually executes. To do this, either launch the script as in:
Code:
bash -x script.sh
or add -x in the sha-bang at the beginning of the script itself:
Code:
#!/bin/bash -x
I suggest to test a reduced version of the script, containing only the ftp part (as shown in your post) and the variable assignments before that.
 
Old 12-29-2009, 08:10 AM   #8
jmsans
LQ Newbie
 
Registered: Dec 2009
Posts: 4

Original Poster
Rep: Reputation: 0
Ouch! Thank you colucix, I misunderstood tuxdev. I thought the restriction of spaces was with the opening token, not the closing one.
I've tested it with the closing EOF at the very beginning of the line and it works.

Thank you!!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script for file transfer bhandu Linux - Newbie 4 06-24-2007 01:37 PM
Find & Transfer to FTP Script DjRakso Linux - Newbie 3 03-01-2007 03:44 AM
shell script to transfer file from ftp bbgtilak Linux - General 1 12-27-2006 07:35 AM
prematurely terminated connection during a large file transfer rob_xx17 Linux - Networking 7 03-22-2006 10:15 AM
Using BASH script to transfer a file via (s)ftp senther Linux - Newbie 2 07-19-2005 04:47 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:26 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration