1752772 Members
4928 Online
108789 Solutions
New Discussion

Re: shell script help

 
rajesh73
Super Advisor

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

10 REPLIES 10
Dennis Handly
Acclaimed Contributor

Re: shell script help (ftp)

>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

rajesh73
Super Advisor

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
Matti_Kurkela
Honored Contributor

Re: shell script help

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
Dennis Handly
Acclaimed Contributor

Re: shell script help (ftp)

>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}')

rajesh73
Super Advisor

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
rajesh73
Super Advisor

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


rajesh73
Super Advisor

Re: shell script help (ftp)

how to simply fy the output

i need data1 data2,
but the output comes
data1
date2
rajesh73
Super Advisor

Re: shell script help (ftp)

pls help me any one this is urgent
Dennis Handly
Acclaimed Contributor

Re: shell script help (ftp)

>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