我想将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

它起作用了,但答案总是要标出来的。

最佳答案

解决了这个问题

# 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应答的临时文件

关于linux - 如何在bash脚本中启动后台lftp session ?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37875796/

10-10 07:03