0

I would like to create a script that will upload a file until the upload uperation will successfull. The script will monitoring the log file. If "not connected" to the server i want to repeat the upload operation until "connected" and "file successfully transferred" Anyone can help me to build the correct one pls. What should i write after if egrep "not...?

 LOGFILE=/home/transfer_logs/$a.log
 First=$(egrep "Connected" $LOGFILE)
 Second=$(egrep "File successfully transferred" $LOGFILE)

  ftp -p -v -i 192.163.3.3 < ../../example.script > ../../$LOGFILE 2>&1

  if
  egrep "Not connected" $LOGFILE; then

  ftp -p -v -i 192.163.3.3 < ../../example.script > ../../$LOGFILE 2>&1

  until
  [[ -n "$first" ]] && [[ -n "$second" ]]; 
  done
  fi

example contains:

  binary
  mput a.txt
  quit 
5
  • 1
    Are you locked into ftp? What about scp? If you absolutely must use ftp, check out expect. It's tailor-made for scripting control of interactive sessions. Commented Nov 27, 2012 at 21:50
  • I'm not sure if the exit status of ftp is useful, but if so, it would be more robust than grepping the output. Commented Nov 27, 2012 at 21:51
  • may be a bad example, I dont know how i should do it. I just want to repeat the upload operation until the file upload will succesfull. Commented Nov 27, 2012 at 21:54
  • what if someone has changed your ftp password on the remote side? You'll lock your account, unless you add more robust error checking. If this is strictly a one off you can make it work. But be advised that just about every OS has different ftp clients and they all follow different rules that you'll need to code for (if you want a generic tool). Also, I think you should be able to just put your ftp cmd inside the until loop proper. Those vars will evaluate false the first time, and then immediately execute the ftp cmd. both will have to pass before you quit. (Thats how until loops work). GdLk Commented Nov 27, 2012 at 22:15
  • Absolute duplicate of Bash about repeat until Commented Nov 27, 2012 at 23:33

1 Answer 1

1
while :; do
    ftp ... > $LOGFILE
    grep -qF Connected $LOGFILE && 
    grep -qF "File successfully transferred" $LOGFILE && break
done
Sign up to request clarification or add additional context in comments.

6 Comments

@Smithis Are you familiar with man pages? RTFM. And, no, the quotes are not necessary. Have you tried the commands given above?
i tried yes, after running it gives nothing back, must ctr+z to stop
What do you mean "it gives nothing back"? Do you mean that it is stuck in the loop continually trying to ftp, and therefore not returning? You mention <ctrl>-z, so I assume you are running on windows; if you're on Unix/Linux, it would be better to use ^C.
Try adding -x to the shebang: #!/bin/bash -x. Or add set -x before the while loop. That is useful for debugging.
it cannot connect to the server but dont know why. and yeah it is stuck
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.