1828484 Members
2731 Online
109978 Solutions
New Discussion

script help

 
SOLVED
Go to solution
Tarek_1
Frequent Advisor

script help

Hi there.. i need to make a script that moves three files via ftp, two of which to one directory and one to another. These files should have these names:
-pDATE.dat
-DDATE.dat
-pkDATE.dat
where date is the current date. So the file has different names every day.
After the ftp these files should be moved to the up directory.

My script actually is:

SPOOLDIR=/data/files/to_send
BKPDIR=/data/files
DESTDIRMK=/data/recieved/mk
DESTDIROP=/data/recieved/op
cd $SPOOLDIR
FTP_FILES
mv *.dat $BKPDIR

FTP_FILES ()
{
echo "####################"
echo "STARTING FTP SESSION"
echo "####################"
ftp -n ipaddress <user username password
ascii
hash
cd $DESTDIR
put .... (files in 2 directories)
bye
eof
}

My problem is how to ftp two files in one directory and the other to another directory.
Thanks in advance for your help.
Tarek

10 REPLIES 10
Daimian Woznick
Trusted Contributor

Re: script help

I apologize in advance if I am not reading your question right.

If the files should be in different directories on the remote system you could use the cd command to change to the other directory. You could also use the put command with the absolute path on both servers (the cd commands would not be needed and you would be ensured that the file is the correct location). However in the instance that the files are in different directories on the local system you would use the lcd command to change the local directory.

Hope this helps.

As a note it is not a secure method to have clear text accounts and passwords in a readable script.
Tarek_1
Frequent Advisor

Re: script help

The files, locally, are in the same directory. When i make the ftp i have to put 2 files in one directory and one in another.
About the password i know it's not very secure, but it's not so important.
About the ftp how can i do?
put p* remotedir
put d* remotedir
put pk* remotedir
but does the * work??
As i told, the files will have the same prefix but then they're different.
Marcin Golembski_1
Honored Contributor

Re: script help

put works with single files, so put p* won't work. But mput p* will.

Daimian Woznick
Trusted Contributor

Re: script help

When sending files via a script it may not be a good idea to use the *. If you plan on setting up a little code to check the output to ensure the files were transferred correctly it may not work correctly because you may be sending additional files that you didn't want to.

However, as mentioned above, you can use the mput command within ftp to send multiple files.
Daimian Woznick
Trusted Contributor

Re: script help

Just a note on the testing to ensure the files were transferred correctly.

You would first send the standard output of the ftp command to a temporary file for later testing. After the ftp session has closed you will go back and check the temp file. This file should contain 3 instances of the successful ftp transfer message (the HPUX server I am connected to now has "Transfer complete"). You should then check the number of instances this message occurs. If it is not 3 then an error message can be sent via e-mail, pager, etc.

However to make these checks accurate you should ensure that only 3 files are being sent. I would put the commands like this:

put $SPOOLDIR/pDATE.dat $DESTDIR/pDATE.dat
put $SPOOLDIR/DDATE.dat $DESTDIR/DDATE.dat
put $SPOOLDIR/pkDATE.dat $DESTDIR/pkDATE.dat

Also note that if this script will be scheduled to run automatically it is not necessary to include hash because that just monitors the traffic.

Hope this helps.
Tarek_1
Frequent Advisor

Re: script help

I can't use put with filename because this changes every day, i have to use * or something like that after the prefix name.
In the to_send directory (locally) i only have those 3 files, nothing else... and after ftp i move the files to the up directory so the day after, when these file are generated, in the to_send directory i will only find again 3 files and so on.
Marcin Golembski_1
Honored Contributor
Solution

Re: script help

You could define one more variable in your script, say DT, based on the current system date. E.g:

DT=`date +%Y-%m-%d`

This would assign DT value of 2002-07-10 (the format I use; see man date for how to change it).

Then you could do

put d$DT.dat remotedir

which would result in sending file d2002-07-10.dat to remotedir.

Stephanie Nicholls
Frequent Advisor

Re: script help

Another way to do this is to use an ftp macro. Macro definitions are stored in the user's .netrc file, so you can have the script generate the macro with the changing file names each day and during the ftp session just invoke the macro.

Additionally there is a special macro type 'init' which is invoked at login, so if you define this macro name ftp will automatically run the commands in the macro for you.

A sample .netrc file would look like:
machine
login # optional - if used ftp will login with this name
password # optional

macdef init # defines initial macro - this is run at login

# end macro with null line

macdef getit # a non 'login' macro, invoked from inside ftp by typing $getit at the ftp prompt.

# end macro with null line

A .netrc file must be in the users home directory and have permissions of 400, obviously there are security risks if you choose to include a cleartext login and password for the remote server.

HTH

Steph
Tarek_1
Frequent Advisor

Re: script help

Hi again.
I have another problem with my script. I explain how my script is made. It is composed of two parts, sql and script shell.
I extract data from oracle with sqlplus and this generates the three files, after that i do ftp to move this files.
Now if i run my script manually (sh scriptname.sh) it works fine and everything's go ok, but if i run the same script with crontab it gives me an error:
Message file spl.msb not found
Error 6 initializing SQL*PLUS
My line in the crontab is:
0 3 * * * sh /tmp/myscript.sh 1>/tmp/myscript'date +'\%y\%m\%d''.log 2>&1
Any hint?
Thanks again.
Marcin Golembski_1
Honored Contributor

Re: script help

cron doesn't read your login files to set the environment variables, especially PATH, you have when logged on interactively. So a command that runs fine from the prompt may not run from cron because the environment is different.

Run `set` command from the prompt and save the output. Then run `set` from a script from cron and compare the output to the saved one. I think you might see a different PATH variable and maybe some other different or missing ones on the "cron" output. If so, try redefining those in your script.