I have to upload a file test_201105281100.txt to a ftp location.
The files will be created on hourly basis like test_201105281100.txt, test_201105281200.txt & so on.
After a file is uploaded successfully, I need to rename the file as test_201105281100.success & if it is not uploaded successfully then rename it as test_201105281100.failure.
Also I need to delete the older files ( I may delete all the files or only .success files). After 12 AM on 30th May, I want to delete all the files that were created on 28th.
I want the script to run as a cron job.
Please help.
Note: I will be able to implement & test the solution that you suggest on Monday morning (more than 36 Hours from now).
I made the changes. but now it gives Not connected message 2 times while connecting to FTP. The User name & password are correct as I am able to connect to FTP manually.
bash-2.03# ftp 97.253.98.14
Connected to 97.253.98.14.
220 L028XRAPDN FTP server ready.
331 Password required for postgres.
230 User postgres logged in.
ftp>
The Username & password are saved in .netrc file.
It is working fine for me.Please check whether your IP is correct and try to FTP manually for that IP.
user@host2> (/home/user) $ cat test.sh
#!/bin/bash
function upload(){
ftp -inv 100.155.34.123 <<EOF
user user pass2000
bin
bye
EOF
}
user@host2> (/home/user) $ cat f.sh
#!/bin/bash
source test.sh
upload
user@host2> (/home/user) $ ./f.sh
Connected to 100.155.34.123.
220 tonga FTP server ready.
334 Using AUTH type GSSAPI; ADAT must follow
GSSAPI accepted as authentication type
GSSAPI error major: Unspecified GSS failure. Minor code may provide more information
GSSAPI error minor: No credentials cache found
GSSAPI error: initializing context
GSSAPI authentication failed
504 AUTH KERBEROS_V4 not supported.
KERBEROS_V4 rejected as an authentication type
331 Password required for user.
230 User user logged in.
200 Type set to I.
221-You have transferred 0 bytes in 0 files.
221-Total traffic for this session was 332 bytes in 0 transfers.
221-Thank you for using the FTP service on tonga.
221 Goodbye.
user@host2> (/home/user) $
I am able to FTP the file if I hard code the filename. But if I use $file_name instead of filename, the file is not uploaded on FTP.
bash-2.03# cat upload.sh
#!/bin/bash
function upload(){
ftp -inv 97.253.98.14 <<EOF
user postgres postgres
bin
put ONLINE_WAIVER_2011052816.txt
bye
EOF
}
bash-2.03#
bash-2.03#
bash-2.03# ./f.sh
Connected to 97.253.98.14.
220 L028XRAPDN FTP server ready.
331 Password required for postgres.
230 User postgres logged in.
200 Type set to I.
200 PORT command successful.
150 Opening BINARY mode data connection for ONLINE_WAIVER_2011052816.txt.
226 Transfer complete.
local: ONLINE_WAIVER_2011052816.txt remote: ONLINE_WAIVER_2011052816.txt
17 bytes sent in 0.00017 seconds (100.62 Kbytes/s)
221-You have transferred 17 bytes in 1 files.
221-Total traffic for this session was 404 bytes in 1 transfers.
221-Thank you for using the FTP service on L028XRAPDN.
221 Goodbye.
bash-2.03#
bash-2.03# echo $file_name
ONLINE_WAIVER_2011052816.txt
bash-2.03#
bash-2.03#
bash-2.03# cat upload.sh
#!/bin/bash
function upload(){
ftp -inv 97.253.98.14 <<EOF
user postgres postgres
bin
put $file_name
bye
EOF
}
bash-2.03#
bash-2.03# ./f.sh
Connected to 97.253.98.14.
220 L028XRAPDN FTP server ready.
331 Password required for postgres.
230 User postgres logged in.
200 Type set to I.
(local-file) (remote-file) 221-You have transferred 0 bytes in 0 files.
221-Total traffic for this session was 201 bytes in 0 transfers.
221-Thank you for using the FTP service on L028XRAPDN.
221 Goodbye.
bash-2.03#
Oh man you made mistake accessing evn variable inside your script.
First when you set a variable at your prompt, like i did a=10, then the same won't be available inside the script you are running.
The prompt shell is a different process and the script you are running is a different process.
If you are planning to share it, you have it to make it as environment variable by using export.
Check the output i have posted, i was not able to access a inside test.sh, but in the prompt i am getting it