I have written below code to get listing from a remote server.

echo "set xfer:log yes" > batchfile.txt
echo "set xfer:log-file Templogfile1.txt">> batchfile.txt
echo "open -p $rport -u $ruser,$rpass sftp://$ripaddress">> batchfile.txt
echo "cache flush">> batchfile.txt
echo "cd $rrdir">> batchfile.txt
echo "pwd">> batchfile.txt
echo "ls -l $srfile">> batchfile.txt
echo "quit">> batchfile.txt

exec 3>&1 4>&2                 
exec > TempSessionLog.txt 2>&1 
set -x                         
  lftp -f batchfile.txt >TempLogFile.log <<- EndFTP 
exec 1>&3 2>&4   
set +x         

I have passed an incorrect directory name. A standard sftp returns error code ($? != 0) but lftp sends a success (retval1 = 0) return code.

In the TempSessionLogFile above, I see the error message cd: Access failed: No such file (<<Directory Path>>) with a successful return value.

Is there a way to capture the errors in lftp on the return codes? If not, what are my alternatives?

1 Answer 1


As long as you aren't doing anything subtle with the FTP protocol, which isn't the case here, the easy way is to use normal copying commands, and mount the FTP server to a directory. You can use LftpFS, which is a FUSE filesystem that uses lftp under the hood. Alternatives include CurlFtpFS. For SFTP rather than FTP, use SSHFS.

trap 'fusermount -u mnt; rmdir mnt' EXIT INT TERM HUP
set -e
mkdir mnt
sshpass -p"$rpass" sshfs "$ruser@$ripaddress:/" mnt
cd "mnt/$rrdir"
ls -l "$srfile"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .