Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Ftp script with date

Shell Programming and Scripting


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 06-27-2017   -   Original Discussion by fretagi
fretagi fretagi is offline
Registered User
 
Join Date: Oct 2011
Last Activity: 16 November 2017, 9:03 AM EST
Posts: 408
Thanks: 98
Thanked 2 Times in 2 Posts
Ftp script with date

Hi

I have the following script that should run every day:

Code:
#!/bin/sh
HOST=192.168.6.47
USER=user123
PASSWD=xyz123L

/usr/bin/ftp -inv <<EOF
open $HOST
user $USER $PASSWD
binary
cd /home/user123/scripts/loadDadosAppWeb/relatorios/25
mput *2017-06-23*
bye
EOF

Bu everyday I have to manually change the line where the date is mput *2017-06-23*.
Can you help me automate that? Instead of editing the script manually everyday
Sponsored Links
    #2  
Old Unix and Linux 06-27-2017   -   Original Discussion by fretagi
wbport wbport is offline
Registered User
 
Join Date: Sep 2013
Last Activity: 15 November 2017, 2:47 PM EST
Location: Mississippi
Posts: 121
Thanks: 34
Thanked 18 Times in 18 Posts
The command
Code:
date +*%Y-%m-%d*

will give you the format you need. Surround it with backtics (``) and it can be the subject of echo or can set the value of a script variable.

HTH
The Following User Says Thank You to wbport For This Useful Post:
fretagi (06-29-2017)
Sponsored Links
    #3  
Old Unix and Linux 06-29-2017   -   Original Discussion by fretagi
fretagi fretagi is offline
Registered User
 
Join Date: Oct 2011
Last Activity: 16 November 2017, 9:03 AM EST
Posts: 408
Thanks: 98
Thanked 2 Times in 2 Posts
that worked fine. But on the same line if I want to transfer the last file of the directory, like from this directory:

Code:
-rw-r--r--   1 rundamo  rundamo   746814 Jun 25 02:20 RundamoSettlement_1498349224609.csv
-rw-r--r--   1 rundamo  rundamo   640038 Jun 26 02:19 RundamoSettlement_1498435659922.csv
-rw-r--r--   1 rundamo  rundamo   645657 Jun 27 02:19 RundamoSettlement_1498522015755.csv
-rwxr-xr-x   1 rundamo  rundamo      202 Jun 27 14:39 ftp_tranf.sh
-rw-r--r--   1 rundamo  rundamo   668370 Jun 28 02:16 RundamoSettlement_1498608378768.csv
-rw-r--r--   1 rundamo  rundamo   670705 Jun 29 02:20 RundamoSettlement_1498694810954.csv
-bash-3.00$

in this case just the :

Code:
-rw-r--r--   1 rundamo  rundamo   670705 Jun 29 02:20 RundamoSettlement_1498694810954.csv

what could I be using
    #4  
Old Unix and Linux 06-29-2017   -   Original Discussion by fretagi
apmcd47 apmcd47 is offline
Registered User
 
Join Date: Feb 2011
Last Activity: 21 November 2017, 11:59 AM EST
Posts: 269
Thanks: 11
Thanked 73 Times in 70 Posts
Quote:
Originally Posted by fretagi View Post
that worked fine. But on the same line if I want to transfer the last file of the directory, like from this directory:

Code:
-rw-r--r--   1 rundamo  rundamo   746814 Jun 25 02:20 RundamoSettlement_1498349224609.csv
-rw-r--r--   1 rundamo  rundamo   640038 Jun 26 02:19 RundamoSettlement_1498435659922.csv
-rw-r--r--   1 rundamo  rundamo   645657 Jun 27 02:19 RundamoSettlement_1498522015755.csv
-rwxr-xr-x   1 rundamo  rundamo      202 Jun 27 14:39 ftp_tranf.sh
-rw-r--r--   1 rundamo  rundamo   668370 Jun 28 02:16 RundamoSettlement_1498608378768.csv
-rw-r--r--   1 rundamo  rundamo   670705 Jun 29 02:20 RundamoSettlement_1498694810954.csv
-bash-3.00$

in this case just the :

Code:
-rw-r--r--   1 rundamo  rundamo   670705 Jun 29 02:20 RundamoSettlement_1498694810954.csv

what could I be using
You could use one of these constructs:

Code:
myfile=$(ls -r $(date +'*%Y-%m-%d*') | head -1)
ls -r $(date +'*%Y-%m-%d*') | read myfile

Also do not put a password in a script! Use the .netrc file for storing your FTP servers:

Code:
machine 192.168.6.47 login user123 password mypass

and make it readable only by yourself.

Code:
#!/bin/sh
HOST=192.168.6.47
cd /home/user123/scripts/loadDadosAppWeb/relatorios/25
myfile=$(ls -r $(date +'*%Y-%m-%d*') | head -1)
/usr/bin/ftp -iv ${HOST}<<EOF
binary
put "${myfile}"
bye
EOF

Andrew
Sponsored Links
    #5  
Old Unix and Linux 06-29-2017   -   Original Discussion by fretagi
fretagi fretagi is offline
Registered User
 
Join Date: Oct 2011
Last Activity: 16 November 2017, 9:03 AM EST
Posts: 408
Thanks: 98
Thanked 2 Times in 2 Posts
I got the following error when running the script:

Code:
230 User oracle logged in
200 Type set to I
250 CWD command successful
?Invalid command
200 Type set to I
(local-file) usage: put local-file remote-file
221 Goodbye.

Sponsored Links
    #6  
Old Unix and Linux 07-17-2017   -   Original Discussion by fretagi
fretagi fretagi is offline
Registered User
 
Join Date: Oct 2011
Last Activity: 16 November 2017, 9:03 AM EST
Posts: 408
Thanks: 98
Thanked 2 Times in 2 Posts
Hi

I am trying a different way of doing that by copying the last file to a directory, them ftp the file to another server, but my cp command is failing with syntax error such as:


Code:
 pwd
/opt/fundamo/reports
-bash-3.00$ ls -lrt FundamoSettlement* | tail -1
-rw-r--r--   1 fundamo  fundamo   604080 Jul 17 02:18 FundamoSettlement_1500250109585.csv
-bash-3.00$ cp -p `ls -lrt FundamoSettlement* | tail -1` /opt/fundamo/reports/tobesent/
cp: illegal option -- w
cp: illegal option -- -
cp: illegal option -- -
cp: illegal option -- -
cp: illegal option -- -
cp: illegal option -- -
Usage: cp [-f] [-i] [-p] [-@] f1 f2
       cp [-f] [-i] [-p] [-@] f1 ... fn d1
       cp -r|-R [-H|-L|-P] [-f] [-i] [-p] [-@] d1 ... dn-1 dn
-bash-3.00$

please can you help?
Sponsored Links
    #7  
Old Unix and Linux 07-17-2017   -   Original Discussion by fretagi
apmcd47 apmcd47 is offline
Registered User
 
Join Date: Feb 2011
Last Activity: 21 November 2017, 11:59 AM EST
Posts: 269
Thanks: 11
Thanked 73 Times in 70 Posts
Quote:
Originally Posted by fretagi View Post
Hi

I am trying a different way of doing that by copying the last file to a directory, them ftp the file to another server, but my cp command is failing with syntax error such as:


Code:
 pwd
/opt/fundamo/reports
-bash-3.00$ ls -lrt FundamoSettlement* | tail -1
-rw-r--r--   1 fundamo  fundamo   604080 Jul 17 02:18 FundamoSettlement_1500250109585.csv
-bash-3.00$ cp -p `ls -lrt FundamoSettlement* | tail -1` /opt/fundamo/reports/tobesent/
cp: illegal option -- w
cp: illegal option -- -
cp: illegal option -- -
cp: illegal option -- -
cp: illegal option -- -
cp: illegal option -- -
Usage: cp [-f] [-i] [-p] [-@] f1 f2
       cp [-f] [-i] [-p] [-@] f1 ... fn d1
       cp -r|-R [-H|-L|-P] [-f] [-i] [-p] [-@] d1 ... dn-1 dn
-bash-3.00$

please can you help?
The problem is you are passing the output of ls -l into cp. The cp command is effectively:

Code:
cp -rw-r--r--   1 fundamo  fundamo   604080 Jul 17 02:18 FundamoSettlement_1500250109585.csv /opt/fundamo/reports/tobesent/

Try
Code:
cp $(ls -1t FundamoSettlement* | head -1) /opt/fundamo/reports/tobesent/

In this case ls will list only the filenames. By removing the -r from ls and using head instead of tail you may also reduce time, particularly if the directory has hundreds of matching files.

Andrew
The Following User Says Thank You to apmcd47 For This Useful Post:
fretagi (07-17-2017)
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Script to copy creation date over top of modified date? toysareforboys Shell Programming and Scripting 3 04-14-2016 10:48 AM
Script to determine Date,TotalFile,total size of file based on date krish2014 Shell Programming and Scripting 5 12-02-2014 04:07 AM
Shell script to compare two files of todays date and yesterday's date hemanthsaikumar Shell Programming and Scripting 4 12-01-2014 09:05 AM
Need help in Shell Script comparing todays date with Yesterday date from Sysdate kumarmsk1331 Shell Programming and Scripting 0 07-29-2011 05:49 PM
How to increment a user defined date value in the DATE format itself using shell script? sunil087 Shell Programming and Scripting 1 12-22-2009 06:29 AM



All times are GMT -4. The time now is 04:51 PM.