Bash about repeat until

2019-08-30 10:38发布

问题:

I want to know about that syntax is correct or not. I cant test it right now sorry, but its important for me. Its an FTP script. The file name is a.txt, I would like to create a script that will upload a file until it is successful. It will works or not? Anyone can help me to build the correct one pls

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 contains:

 binary
 mput a.txt
 quit 

回答1:

Does ftp not return a reasonable result? It would be easiest to write:

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

If you insist on searching the log file, do something like:

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

Or

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


回答2:

It will works or not?

No, it won't work. According to §3.2.4.1 "Looping Constructs" of the Bash Reference Manual, these are the kinds of loops that exist:

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

You'll notice that none of them begins with repeat.

Additionally, these two lines:

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

run egrep immediately, and set their variables accordingly. This command:

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

will always give the same return-value, because nothing in the loop will ever modify $firstline and $secondline. You need to actually put an egrep command inside the loop.



标签: bash shell unix