bash script for ftp-upload is not working

Hello everyone,

sorry for the title, most of you must getting sick of reading something like
this, but I haven't found a solution, although I found many threads according
to it.

I'm working on a bash script that connects to a network printer with ftp
where I want to upload a pdf created with fpdf, which should be printed. This bash script should later be called via cronjob.
But I've already got a problem with the first step.

I've tested the script which I found with the help of google, but my test pdf file
seems not to be fully uploaded until the script ends.

I have read, that when using a "here document", the commands should be
executed as if they were typed, but the put-command seems to be aborted as
the last few bytes seems to be missing.
I tried the sleep-command after the put,but then I receive an "?Invalid
Command" error.

the primary script:

#!/bin/bash
USER='xxx'
PASSWD='xxx'

ftp -n <<EOD
open xxx
quote USER $USER
quote PASS $PASSWD
put /path/to/test.pdf
quit
EOD
exit 0

When I do those steps in the terminal, the network printer prints my
test.pdf.

I would be very thankful if someone could help me, or can show me a
related thread. Everyting I found already, couldn't help me.

greetings

I copy/pasted your code snippet into a new bash script, changed the username, password, testfile, and FTP server, then it ran just fine.

Try changing the first line to:

#!/bin/bash -xv

You can get a better idea of where it's failing.

You can't put a "sleep" between the "<<EOD" and "EOD" since FTP doesn't understand that command.

If you're missing bytes, you probably need to issue a "bin" (binary) command prior to your "put", otherwise the end-of-line character can get affected. FTP transfers over ASCII mode by default.

#!/bin/bash
USER='xxx'
PASSWD='xxx'

ftp -n <<EOD
open xxx
quote USER $USER
quote PASS $PASSWD
bin
put /path/to/test.pdf
quit
EOD
exit 0

Also, the FTP "put" command with a single argument implies the location of the local file is the same as the remote file, so if you do this:

put /path/to/file.txt

You are telling FTP to upload the local file /path/to/file.txt to the FTP's remote directory /path/to/, which may not exist. Perhaps you mean to do this:

put /path/to/file.txt file.txt

-dufftime

1 Like

FTP defaults to "binary". Try the ftp "status" command just after logging in.
Many versions of ftp will try to deduce the transfer type based on the MSDOS style filename (e.g. .txt) unless told otherwise. Always good to postively state ascii or binary.

Let's check that the script is in the correct unix text file format. This sed is designed to make spaces and control characters visible.

sed -n l scriptname

Many laser printers need a formfeed character at the end of the document. Otherwise they wait until the connection times out before emptying the print buffer.

Thank you both so much!

It works now, at least when I try it from my laptop.
I'll try it over night now on the server with the cronjob.

Although the terminal "told" me, that it was using binary mode when applying
my primary script, the idea with the bin-command before using the
put command seemed to make the difference.

You, sir, made my day.