0

I have a KornShell (ksh) script which writes out FTP script and executes to get some files from windows server to UNIX.

echo "user <username> <pwd>" >ftpin.out
echo "bin">>ftpin.out
echo "get file.txt" >>ftpin.out
echo "get file2.txt" >>ftpin.out
echo "!echo $?">>ftpin.out
echo"bye">>ftpin.out

ftp -n -i servername <ftpin.out
echo $?

Here I am not able to check if the file exists on the source Windows system or not. Because this usual error handling is not working and the $? is 0 always when any file is there and other is not there. I even tried checking for ERRORLEVEL but that is not working on UNIX(Solaris 10).

Can any one show samples of simple error handling for this which works or how to do it? Log file parsing was also taken into consideration but is there any other built in way? Other FTP mechanisms are out of scope. We are using plain old FTP.

2
  • Stop using ftp and switch to wget, curl or lftp.
    – Fred Foo
    Jan 10, 2011 at 13:49
  • Sorry... my current organization doesn't want that. I am helpless in this. Jan 10, 2011 at 13:58

2 Answers 2

1

I don't think reading $? will work. You just get the value from the subshell executed (with the ! command).

I suggest you could take a look at expect: http://expect.sourceforge.net/ It's free and probably available in your Linux distribution's repository.

From the web page:

Expect is a tool for automating interactive applications such as telnet, ftp, passwd, fsck, rlogin, tip, etc.

1
  • Thanks for your response. But as I mentioned I am currently restricted to use ftp itself without any chance of installing any tools in my current environment. Jan 10, 2011 at 13:50
0

Thanks for your responses. I am using the below method to make it work.

echo "user <username> <pwd>" >ftpin.out
echo "bin">>ftpin.out
echo "get file.txt" >>ftpin.out
echo "! [ ! -f file.txt ] && ksh alertme.sh" >>ftpin.out
echo "get file2.txt" >>ftpin.out
echo "! [ ! -f file2.txt ] && ksh alertme.sh" >>ftpin.out
echo"bye">>ftpin.out

ftp -n -i servername <ftpin.out

By the above method if FTP fails to copy one file if the file is not present in the source server then i will call alertme.sh script to mail me an error message. This way I can even use it to overwrite the ftpin.out itself to stop the next get of a file in ftp. I am going to test the below one for this.

echo "! [ ! -f file.txt ] && echo bye>>ftpin.out" >>ftpin.out

Thanks again. Hope this helps for any body facing similar difficulty.

0

Your Answer

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

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