Re: shell script help (ftp) (682 Views)
Reply
Super Advisor
rajesh73
Posts: 431
Registered: ‎03-17-2009
Message 1 of 11 (799 Views)

shell script help

i was write one script for file transfer

 

script


ftp -d -n -i -v 10.40.40.26 <<EOT
user admin Welcome456
lcd
put *.txt

 


the above sciprt upload *.txt file to one window FTP account. i want to upload the (/home/rajesh )in this path we have 5 files , i want to upload today created files not folder , how to write the script

Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,853
Registered: ‎03-06-2006
Message 2 of 11 (791 Views)

Re: shell script help (ftp)

[ Edited ]

>I want to upload today created files

 

You can put the files modified today:

ftp -n -i -v 10.40.40.26 <<EOF
user admin Welcome456

prompt

lcd directory

mput $(find . -type f -mtime -1)

EOF

Please use plain text.
Super Advisor
rajesh73
Posts: 431
Registered: ‎03-17-2009
Message 3 of 11 (769 Views)

Re: shell script help

diff file1 file2 > file3

more file3
Only in file2: data

i want to tranfer the file called data


pls find the below command is not working, please help me

put $file3
Please use plain text.
Honored Contributor
Matti_Kurkela
Posts: 6,271
Registered: ‎12-02-2001
Message 4 of 11 (747 Views)

Re: shell script help

[ Edited ]

So you're finding the differences between file1 and file2, and saving the list of differences to file3.

You show that the content of file3 is literally:

Only in file2: data

 

Your command "put $file3" does not work because there is no environment variable named "file3": environment variables are not the same as files.

 

The diff command also produces output like "Only in <name>: <filename>" only when <name> is a directory. So I guess file1 and file2 are actually directories.

 

A different syntax could be used to read the contents of file3 and use them on the command line. There are two ways to do this:

put $(cat file3)
put $(< file3)

If file3 has the content you showed, this would result in a command like "put Only in file2: data", which will not work. You would have to remove the "Only in file2: " part before using the rest to build your "put" command. And if the current working directory of the "put" command is the same as when you ran the "diff" command, the command you actually need is more likely "put file2/data".

 

If it is guaranteed that the diff command will always identify one and only one file for transfer in directory file2, you might do something like this:

put file2/$(grep "^Only in file2:" <file3 | awk '{print $4}')

 

 To understand this, you must look inside the $( ... ) construct first.

First, the grep command will find the right line in file3, ignoring any other lines in the file, and piping the right line to awk. The awk command will pick the 4th word from the line, ignoring everything else. This should be the word "data". Now, the entire $( ... ) construct will be replaced with the awk output, i.e. the command line will ultimately be equivalent to:

put file2/data

If there might be more than one file to transfer, you would need to do something like this: (the reason why there was no need to put ../file3 instead of file3 is left as an exercise for the reader :-)

diff file1 file2 > file3
ftp -n -i -v 10.40.40.26 <<EOT
user admin Welcome456
prompt
lcd file2 mput $(grep "^Only in file2:" <file3 | awk '{print $4}')
EOT
MK
Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,853
Registered: ‎03-06-2006
Message 5 of 11 (737 Views)

Re: shell script help (ftp)

[ Edited ]

>There are two ways to do this:

 

No need to mention using an evil cat.  ;-)  $(< file3) is the "right" way for a real shell.

 

You could combine the diff, grep and awk into one:
mput $(diff file1 file2 | awk '/^Only in file2:/ {print $4}')

Please use plain text.
Super Advisor
rajesh73
Posts: 431
Registered: ‎03-17-2009
Message 6 of 11 (720 Views)

Re: shell script help (ftp)

thanks dennis and matti,

from mattia advise today i created the script , but mput not upload two file, it will upload only one file.

dennis tomorrow i try your script , if any doubt i will touch you ,thanks once again
Please use plain text.
Super Advisor
rajesh73
Posts: 431
Registered: ‎03-17-2009
Message 7 of 11 (705 Views)

Re: shell script help (ftp)

i am using yout script, but difference output given two files , but only one file is uploading

please find the output

diff wel sur | awk '/^Only in sur:/ {print $4}'
data1
data2

but we when he execute the scruipt data1 only upload.

pls advise


Please use plain text.
Super Advisor
rajesh73
Posts: 431
Registered: ‎03-17-2009
Message 8 of 11 (703 Views)

Re: shell script help (ftp)

how to simply fy the output

i need data1 data2,
but the output comes
data1
date2
Please use plain text.
Super Advisor
rajesh73
Posts: 431
Registered: ‎03-17-2009
Message 9 of 11 (692 Views)

Re: shell script help (ftp)

pls help me any one this is urgent
Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,853
Registered: ‎03-06-2006
Message 10 of 11 (687 Views)

Re: shell script help (ftp)

[ Edited ]

>but the output comes (separate lines)

 

Oops.  Either we need to change the newline to a space, use printf or just have multiple puts:

ftp -n -i -v 10.40.40.26 <<EOF
user admin Welcome456
lcd file2

$(diff file1 file2 | awk '/^Only in file2:/ {print "put", $4}')      # separate puts

EOF

 

ftp -n -i -v 10.40.40.26 <<EOF
user admin Welcome456
prompt
lcd file2

mput $(diff file1 file2 | awk '/^Only in file2:/ {printf "%s ", $4}')  # combine lines

EOF

 

 

Please use plain text.
Super Advisor
rajesh73
Posts: 431
Registered: ‎03-17-2009
Message 11 of 11 (682 Views)

Re: shell script help (ftp)

thank you so much dennis, script working fine
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation