ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.