使用LFTP回显当前文件传输

时间:2016-11-08 12:45:01

标签: linux bash shell ftp lftp

传输完成后,我正在尝试回显$ file_name。但是我找不到任何关于如何在LFTP中创建变量的参考,该变量显示刚刚下载的文件名。

代码:

#!/bin/bash
login="myusername"
pass="notmypassword"
host="my.hosting.server"
remote_dir='/Path/To/Remote/Dir/.'
local_dir="/Path/To/Local/Dir/"

file_name=**the name of the file im downloading**
base_name="$(basename "$0")"

lock_file="/tmp/$base_name.lock"
trap "rm -f $lock_file" SIGINT SIGTERM
if [ -e "$lock_file" ]
then
    echo "$base_name is running already."
    exit
else
    touch "$lock_file"
    lftp -u $login,$pass $host << EOF
    set ftp:ssl-allow no
    set mirror:use-pget-n 5
    mirror -c -x "\.r(a|[0-9])(r|[0-9])$" -P5 --log="/var/log/$base_name.log" "$remote_dir" "$local_dir"
    echo $file_name
    quit
EOF
    #osascript -e 'display notification "$file_name Downloaded" with title "Media Server"'
    rm -f "$lock_file"
    trap - SIGINT SIGTERM
    exit
fi

我认为回显当前文件然后将此变量添加到我的osascript中以触发OSX上已成功传输文件的通知会相当简单,但对于我的生活,我无法想象如何。

我做错了什么???

干杯!

2 个答案:

答案 0 :(得分:1)

此脚本将回显已传输的每个文件,随时清理并修改它以满足您的需求:

#!/bin/bash

echo "Script started."

download_directory="${HOME}/downloads/"
echo "Downloading to ${download_directory}"

rm ${download_directory}/*

download_log=$(mktemp)
lftp <<- EOF > ${download_log}
    # elided connection details, enter your own
    open 
    mirror -v . ${download_directory}
    quit
EOF

cat ${download_log} | awk ' { print $3 } ' | sed 's/`//' | sed 's/'\''//' | xargs echo "Downloaded:"
rm -f ${download_log}
echo "Script successfully ended."

示例输出:

=> ./foobar.sh  
Script started.
Downloading to /home/downloads/
Downloaded: something1.yes testfile.txt
Script successfully ended.

答案 1 :(得分:0)

使用文件验证脚本:

set xfer:verify on
set xfer:verify-command /home/user/bin/file-transferred

脚本输出(FD 1,2)被重定向,但您可以使用/dev/tty将文件名输出到终端:

#!/bin/sh
echo "$1" >/dev/tty

或者,打开另一个描述符:

lftp 3>&1 <<EOF
...
EOF

并在那里重定向输出:

#!/bin/sh
echo "$1" >&3