This is a discussion on Script that checks for existence of file on remote server. within the AIX Operating System forums, part of the Unix Operating Systems category; --> Hi - I would like to know if there is a way in shell scripting to check the existence ...
| |||||||
| Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| Hi - I would like to know if there is a way in shell scripting to check the existence of a file after FTP? Basically if the file does not exist in a certain directory, the shell script should fail. thanks, val |
| |||
| vegas_girlie <minnie_valerie@hotmail.com> wrote: > I would like to know if there is a way in shell scripting to check the > existence of a file after FTP? Basically if the file does not exist in > a certain directory, the shell script should fail. if [ ! -f somefile ]; then exit 1 fi to check for the existence of a certain file. Yours, Laurenz Albe |
| |||
| If I read the message correctly, I suspect Minnie wanted a way to check that the file was FTP'd over onto the REMOTE system, not to check locally...I may be wrong. If you are administrator of both systems and can do remote shell commands, you can simply set up so that you can issue "rsh" commands. Check into the rsh command and .rhosts or hosts.equiv files. If you must use FTP to do this, you can always capture your FTP commands and output to a file, then scan that output file for the expected returned FTP messages. For example, you may see something like the following: ----------------------------------------------------------------------------------- ftp> put sample.dat 200 PORT command successful. 150 Opening data connection for sample.dat. 226 Transfer complete. 5098 bytes sent in 0.01821 seconds (273.4 Kbytes/s) local: sample.dat remote: sample.dat ftp> quit 221 Goodbye. ------------------------------------------------------------------------------------- You could use "grep" to scan for "^226 Transfer complete." or look for the "bytes sent" line and grab the number of bytes, then compare it to the one you have locally. I believe they are usually the same size... Just some ideas for you to think about ;-) Steve |
| |||
| Hi Steven - You are correct. I need to check the file that was FTP'd over to make sure they landed ok. I was able to create an output file that captured the file name that was FTP'd. However, my other problem is how to write error codes if the file name does not exist in the output file. Will TEST command work? Doesnt Test just check for existence of files or directories and not words? Thanks. steven_nospam at Yahoo! Canada wrote: > If I read the message correctly, I suspect Minnie wanted a way to check > that the file was FTP'd over onto the REMOTE system, not to check > locally...I may be wrong. > > If you are administrator of both systems and can do remote shell > commands, you can simply set up so that you can issue "rsh" commands. > Check into the rsh command and .rhosts or hosts.equiv files. > > If you must use FTP to do this, you can always capture your FTP > commands and output to a file, then scan that output file for the > expected returned FTP messages. For example, you may see something like > the following: > ----------------------------------------------------------------------------------- > ftp> put sample.dat > 200 PORT command successful. > 150 Opening data connection for sample.dat. > 226 Transfer complete. > 5098 bytes sent in 0.01821 seconds (273.4 Kbytes/s) > local: sample.dat remote: sample.dat > ftp> quit > 221 Goodbye. > ------------------------------------------------------------------------------------- > You could use "grep" to scan for "^226 Transfer complete." or look for > the "bytes sent" line and grab the number of bytes, then compare it to > the one you have locally. I believe they are usually the same size... > Just some ideas for you to think about ;-) > > Steve |
| |||
| "vegas_girlie" <minnie_valerie@hotmail.com> wrote in message news:1104947445.075923.122430@c13g2000cwb.googlegr oups.com... > Hi Steven - > You are correct. I need to check the file that was FTP'd over to make > sure they landed ok. > > I was able to create an output file that captured the file name that > was FTP'd. However, my other problem is how to write error codes if > the file name does not exist in the output file. Will TEST command > work? Doesnt Test just check for existence of files or directories and > not words? > > Thanks. > > steven_nospam at Yahoo! Canada wrote: > > If I read the message correctly, I suspect Minnie wanted a way to > check > > that the file was FTP'd over onto the REMOTE system, not to check > > locally...I may be wrong. > > > > If you are administrator of both systems and can do remote shell > > commands, you can simply set up so that you can issue "rsh" commands. > > Check into the rsh command and .rhosts or hosts.equiv files. > > > > If you must use FTP to do this, you can always capture your FTP > > commands and output to a file, then scan that output file for the > > expected returned FTP messages. For example, you may see something > like > > the following: > > > -------------------------------------------------------------------------- --------- > > ftp> put sample.dat > > 200 PORT command successful. > > 150 Opening data connection for sample.dat. > > 226 Transfer complete. > > 5098 bytes sent in 0.01821 seconds (273.4 Kbytes/s) > > local: sample.dat remote: sample.dat > > ftp> quit > > 221 Goodbye. > > > -------------------------------------------------------------------------- ----------- > > You could use "grep" to scan for "^226 Transfer complete." or look > for > > the "bytes sent" line and grab the number of bytes, then compare it > to > > the one you have locally. I believe they are usually the same size... > > Just some ideas for you to think about ;-) > > > > Steve > Might be way off base here, but ssh is secure, free and much easier to script and check results. In today's world, ftp, telnet and rsh should be banned and removed from all servers to help protect our personal security. Then again, just my 2 cents worth. Have a good day Doug |
| |||
| If you are going to use FTP, here's a method that may work: ftp -i ${DESTHOST} << END >> ${TMPOUT} verbose prompt lcd ${SRCDIR} cd ${DESTDIR} pwd put ${SRCFILE} ${DESTFILE} quit END BYTES_SENT=$(cat ${TMPOUT} | grep "bytes sent" | cut -f1 -d ' ') BYTES_ORIG=$(ls -l $(SRCFILE} | awk '{ print $5 }') if test "${BYTES_SENT}" = "${BYTES_ORIG}" then echo "File is same size on remote host - successful transfer" else echo "File sizes differ:" echo "Original:\t${BYTES_ORIG}" echo "Remote:\t${BYTES_SENT}" fi Of course, I'm not sure if the size on the remote is ALWAYS going to be the same as on the local system. That is something you'd have to evaluate. Also , you may want to consider secure shell (ssh) as many people are disabling the FTP and other services to prevent unauthorized logins. If these two systems are internal, then it is not as critical but may still be prone to hacking.... Hope this helps... Steve |
| |||
| steven_nospam at Yahoo! Canada <steven_nospam@yahoo.ca> wrote: > If you are going to use FTP, here's a method that may work: > > ftp -i ${DESTHOST} << END >> ${TMPOUT} > > BYTES_SENT=$(cat ${TMPOUT} | grep "bytes sent" | cut -f1 -d ' ') > BYTES_ORIG=$(ls -l $(SRCFILE} | awk '{ print $5 }') > > if test "${BYTES_SENT}" = "${BYTES_ORIG}" > then > echo "File is same size on remote host - successful transfer" > else > echo "File sizes differ:" > fi > > Of course, I'm not sure if the size on the remote is ALWAYS going to be > the same as on the local system. That is something you'd have to > evaluate. One way to make sure that the size on the destination system will be the same is to use binary mode file transfer. If you have to transfer ASCII files, and the destination system does not run UNIX, you will need ascii mode file transfer. You can force binary mode by issuing 'binary' before the 'put' command and ascii mode by issuing 'ascii'. If you use ascii file transfer, the size of the file on the receiving end could be different; e.g. if you transfer from UNIX to Windows, an ASCII 0x0d character (carriage return) will be inserted before every line feed (ASCII 0x0a), thereby enlarging the destination file. In that case I see two possibilities: a) forget about the file size and check the output file for the message that indicates successful transfer. b) convert the file to the correct ascii format on the destination platform first and then transfer it in binary mode. For UNIX to Windows transformation, you could use the 'doswrite' command that is shipped with AIX. Yours, Laurenz Albe |
| |||
| The verbose command worked. Thanks! however, i would now like to search for the number of occurrences of '226 Transfer complete' and if it is less than 46 occurrences fail the jobs. I have code that is something like this but is not working. Any advice would be greatly appreciated. Count=$(grep '226 Transfer complete.' file.txt) if (( $Count < 46)) then echo 'missing ftp files' exit 1 fi can |
| ||||
| vegas_girlie <minnie_valerie@hotmail.com> wrote: > however, i would now like to > search for the number of occurrences of '226 Transfer complete' and if > it is less than 46 occurrences fail the jobs. > > Count=$(grep '226 Transfer complete.' file.txt) Try something like Count=$(grep '226 Transfer complete.' file.txt|wc -l) Yours, Laurenz Albe |