0

I have bash script ftp upload file. In log file I have all events. How can I have in log file only several events, like: "Connected to $HOST" or "WARNING - failed ftp connection"; "Ok to send data"; "Transfer complete"; "10000 bytes sent in 0.00 secs (73.9282 MB/s)".

#!/bin/sh
echo "####################" >> $TESTLOG
echo "$(date +%Y%m%d_%H%M%S)" >> $TESTLOG

ftp -i -n -v <<SCRIPT >> ${TESTLOG} 2>&1
open $HOST
user $USER $PASSWD
bin
cd $DPATH
lcd $TFILE
mput *.txt
quit
SCRIPT

exit 0
####################
20210304_111125
Connected to $HOST.
331 Please specify the password.
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
200 Switching to Binary mode.
250 Directory successfully changed.
Local directory now /home/pi/Data/data_files/tmp
local: 20210304_111125_ftp_10k.txt remote: 20210304_111125_ftp_10k.txt
200 PORT command successful. Consider using PASV.
150 Ok to send data.
226 Transfer complete.
10000 bytes sent in 0.00 secs (73.9282 MB/s)
221 Goodbye.
Oleg
  • 3
  • 3

1 Answers1

0

First, I'd like to say while this is a very common idiom, it has no error checking and I despise it.

Now that I got that off my chest...

ftp 2>&1 -inv << SCRIPT | grep -f patfile >> ${TESTLOG}

patfile is a list of patterns to keep. c.f. the grep manual page.

...to continue my rant, though...

What if someone changes the permissions on your $DPATH? The cd fails; ftp reports the failure to the console (your grep ignores it so it doesn't even get logged...), the script continues and puts the files in the wrong place. Full disk prevents files from being placed? Same cycle. A thousand things could go wrong, but ftp just blithely goes on, and doesn't even return an error on exit for most of them. Don't do this.

Just use scp. Yes, you have to set up something like ssh keys, but then the command is simple and checkable.

scp $TFILE/*.txt $HOST:$DPATH/ || echo "copy failed"

For a more elaborate response -

if scp $TFILE/*.txt $HOST:$DPATH/  # test the scp
then ls -ltr $HOST:$DPATH/*.txt    # show result if success
else echo "copy failed"            # code here for if it fails...
     exit 1                        # as much code as you feel you need
fi

Otherwise, use a different language with an ftp module like Perl so you can check steps and handle failures.

(I wrote a ksh script that handled an ftp coprocess, fed it a command at a time, checked the results... it's doable, but I don't recommend it.)

Paul Hodges
  • 8,723
  • 1
  • 12
  • 28
  • Thank you. I will try to implement it via ``scp``, to get a check of the sent files. Now all works via ``ftp``and is written to the log on the local machine. I don't worry about security, because there is no external access and all rights are limited. If you have already answered someone on the implementation of the check through the ``scp``. Please send a link me for review. Thank you for help and advice. – Oleg Mar 04 '21 at 14:45
  • A trap can set up a single global error test - c.f. https://stackoverflow.com/questions/30078281/raise-error-in-a-bash-script/50296873#50296873 for examples. Will edit answer for a simple local test. – Paul Hodges Mar 05 '21 at 15:33