How to test if lftp has worked in bash script

By | July 15, 2016
How to test if lftp has worked in bash script

How to test if lftp has worked in bash script

How to test if lftp has worked in bash script

Hi everyone, one of the things I tend to do a lot when writing Bash Scripts is to create a lot of small and simple checking scripts for various functionality.

I have been using lftp quite frequently with a client of mine for some time now, but one of the errors more junior coders do is to just check $? to establish whether the previous command has worked or failed.

The famous test bash code is –

if [ $? -eq 0 ]
then
echo "success"
exit 0
else
echo "failed"
exit 1
fi

BUT like I said that tests what exit code the previous command has sent you. In the case of lftp (and MANY other commands) all it is telling you is that the lftp command started, NOT whether the file you wanted to send to your remote host has actually worked or not.

So here is a very simple script that will give you the correct exit code for you to then decide how you want to proceed if for example your lftp has failed. I have called the script lftp_check.bash

#!/bin/bash
 #
 # Utility to check lftp to ensure data has been transferred
 #
 # $1 - lftp output file
 #
 # Script will exit with 0 for success, 1 for error, 2 for no logfile found
 #
 # Example -
 #
 # [root@serv tmp]# echo test > test.txt
 # [root@serv tmp]# lftp "sftp://${FTP_USER1}:${FTP_PASS1}@${FTP_HOST1}" -e "cd ${FTP_DEST1};put text.txt;bye" > /tmp/lftp_output.txt 2>&1
 # [root@serv tmp]# cat /tmp/lftp_output.txt
 # cd ok, cwd=/destination_dir
 # put: /tmp/text.txt: No such file or directory
 # [root@serv tmp]# lftp "sftp://${FTP_USER1}:${FTP_PASS1}@${FTP_HOST1}" -e "cd ${FTP_DEST1};put test.txt;bye" > /tmp/lftp_output.txt 2>&1
 # [root@serv tmp]# cat /tmp/lftp_output.txt
 # cd ok, cwd=/destination_dir
 # 5 bytes transferred
 # [root@serv tmp]#
 #
 # Written by Malc on 14.07.2016
 #

LFTP_FILE="$1"

if [ -f "${LFTP_FILE}" ]
 then
 grep "bytes transferred" "${LFTP_FILE}" > /dev/null
 if [ $? -eq 0 ]
 then
 echo "lftp successful"
 exit 0
 else
 echo "lftp error"
 exit 1
 fi
 else
 echo "$LFTP_FILE not found"
 exit 2
 fi

So a little explanation –

Check out -  Bash remove duplicate lines without sorting

1) Firstly you already have your lftp command, you need to capture the output from it so the check script can analyse it

lftp "sftp://${FTP_USER1}:${FTP_PASS1}@${FTP_HOST1}" -e "cd ${FTP_DEST1};put text.txt;bye" > /tmp/lftp_output.txt 2>&1

2) Now you can tell lftp_check.bash to use /tmp/lftp_output.txt and tell you if the transfer has worked or failed

/usr/local/bin/lftp_check.bash /tmp/lftp_output.txt

The script itself is very simple, the output shows the text ‘bytes transferred‘ which means your file has been sent (or received). You can then check the exit code $? like the example above to determine if your lftp has worked (exit code 0), or failed (exit code 1), or it couldnt find your lftp output file to read in the first place (exit code 2).

For my purposes that is all I need, but it would not be hard to take the script a step further to capture the size of the data sent and match it to the file you were sending and compare them.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.