Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I would like to create a loop that repeats a ncftp transfer if it returns an error.

I'm a little unsure how the exit code variable can be used in a loop. Would something like this work?

until [$? == 0]; do
    ncftpput -DD -z -u user -p password remoteserver /remote/dir /local/file
share|improve this question
up vote 22 down vote accepted

I found the basis for this elegant loop elsewhere on serverfault. Turns out there is no need save the exit code, as you can test directly on the command itself;

until ncftpput -DD -z -u user -p password remoteserver /remote/dir /local/file; do
  echo Tansfer disrupted, retrying in 10 seconds...
  sleep 10
share|improve this answer

Almost. You are probably better saving the return value as a variable so that you can pre-set it before the loop. Otherwise it will be affected by the last-run command.

You might also want to sling a sleep in there to stop it respawning too quickly.

until [ ${RET} -eq 0 ]; do
    ncftpput -DD -z -u user -p password remoteserver /remote/dir /local/file
    sleep 10
share|improve this answer
I think forgot to pass the exit code to the RET variable, but I get the idea. My main question is when will the until clause be evaluated? In C it won't be evaluated until after the first run of the loop, in which case my loop should work. Yours is still better, though, as it allows for intermediate commands such as the sleep. Thanks! – Roy Nov 3 '09 at 9:33
Well spotted. I've edited it. The clause will be evaluated before the first run. So previously it would use the return code of any preceding command. The double-equals aren't enforced by Bash but it makes good practice to use. – Dan Carley Nov 3 '09 at 9:51
== is for string equality not numeric, although I don't think it matters in this case... – Kyle Brandt Nov 3 '09 at 13:01
Single and double equals are the same. But it would be better to use integer equality now that you mention it. – Dan Carley Nov 3 '09 at 13:33
You don't need to test the return code number -- you can just test the return code of the program itself -- until program ; do sleep 3 ; done . – chris Nov 3 '09 at 22:15

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.