如何在bash脚本中启动后台lftp会话?

时间:2016-06-17 07:39:33

标签: linux bash stdout

我想将FTP会话与文件描述符相关联,文件描述符将在整个脚本中引用它。包括周期。

例如像这样的东西。但它没有做到这一点。

#!/bin/bash
#start of script
exec {ftpdescriptor}<> >(lftp -u $ftpuser,$ftppass $ftpip/$ftptd)

# code

(echo "ls" 1>&"$ftpdecriptor")> myanswer

# code

echo "bye" 1>&"$ftpdecriptor"
exec {ftpdescriptor}>&-
exit 0
# end of script

它有效,但答案总是在stdout ..

1 个答案:

答案 0 :(得分:0)

解决了这个问题

# start of script
ftpout=$(mktemp)
$timetowaitftp
exec {ftpin}> >(lftp -u $ftpuser,$ftppass $ftpip/$ftptd > $ftpout)
printf >&$ftpin "set net:timeout 10\n"
function ftpio {
    :>$ftpout
    printf >&$ftpin "$1\n"
    i=0
    while [ ! $2 ] && [ ! -s $ftpout ] && [ $i -lt 10 ]; do
        # echo "waiting answer from ftp 1 sec.."
        sleep 1;
        let i=i+1
    done
}

# code

ftpio "cd /modx" "nowait" # no output of cd command.
ftpio "ls"
cat $ftpout
sleep 15
ftpio "pwd"
cat $ftpout
#ftpio "put /var/www/vhosts/modx/backups/20160121.113318.tar.gz" "nowait" # 12Gb
#ftpio "put /var/www/vhosts/modx/backups/20150930.092338.tar.gz" "nowait" # 800mb

# /code

# end of script
printf 1>&"$ftpin" "bye\n"
exec {ftpin}>&-
rm $ftpout

ftpin - 命名描述符

ftpout - 最后一个回答为ftp的临时文件