猛击重复直到

时间:2012-11-27 19:02:49

标签: bash shell unix

我想知道语法是否正确。我现在不能测试对不起,但它对我很重要。它是一个FTP脚本。文件名是a.txt,我想创建一个脚本,上传文件直到成功。它会起作用吗?任何人都可以帮我建立正确的一个

LOGFILE=/home/transfer_logs/$a.log
DIR=/home/send
Search=`ls /home/send`
firstline=`egrep "Connected" $LOGFILE`
secondline=`egrep "File successfully transferred" $LOGFILE`

if [ -z "$Search" ]; then
cd $DIR
ftp -p -v -i 192.163.3.3 < ../../example.script > ../../$LOGFILE 2>&1
fi

if
egrep "Not connected" $LOGFILE; then
repeat
ftp -p -v -i 192.163.3.3 < ../../example.script > ../../$LOGFILE 2>&1
until
[[ -n $firstline && $secondline ]]; 
done
fi

example.script包含:

 binary
 mput a.txt
 quit 

2 个答案:

答案 0 :(得分:2)

  

它会起作用吗?

不,它不会起作用。根据{{​​3}},这些是存在的循环类型:

until test-commands; do consequent-commands; done

while test-commands; do consequent-commands; done

for name [ [in [words …] ] ; ] do commands; done

for (( expr1 ; expr2 ; expr3 )) ; do commands ; done

您会注意到它们都不以repeat开头。

另外,这两行:

firstline=`egrep "Connected" $LOGFILE`
secondline=`egrep "File successfully transferred" $LOGFILE`

立即运行egrep ,并相应地设置其变量。这个命令:

[[ -n $firstline && $secondline ]]

将始终提供相同的返回值,因为循环中的任何内容都不会修改$firstline$secondline。您需要在循环中实际放置一个egrep命令。

答案 1 :(得分:1)

ftp不会返回合理的结果吗?写起来最容易:

while ! ftp ...; do sleep 1; done

如果您坚持搜索日志文件,请执行以下操作:

while :; do
    ftp ... > $LOGFILE
    grep -qF "File successfully transferred" $LOGFILE && break
done

或者

while ! test -e $LOGFILE || grep -qF "Not connected" $LOGFILE; do
    ftp ... > $LOGFILE
done