Solved

sftp script quotes

Posted on 2014-10-29
17
155 Views
Last Modified: 2014-10-31
I need to upload a file and them move the file locally to an archive directory.
I seem to be missing " " around the mv statement but am not sure where to place them
Thank you : )

#!/usr/bin/expect

spawn /usr/bin/sftp username@192.168.1.40
#sleep 5

send "mput /directory_name/*.* \n"
mv /directory_name/*.xml /directory_name/archive/ \n

send "bye\n"
0
Comment
Question by:Peter Kuczynski
  • 9
  • 4
  • 3
  • +1
17 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 40411140
What was the command intended to do?
Did you want to send it to the sftp process?
0
 
LVL 1

Author Comment

by:Peter Kuczynski
ID: 40411178
the command is intended to
1. upload a file to a sftp server, this works
then
2. move the local file to a archive folder retaining it's name

I made this change, so now the command runs but it does not move the file to the local archive directory.

expect "sftp>"
send "mput /directory_name/*.* \n"
send "lmove /directory_name/*.xml /directory_name/archive/ \n"
0
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 40411210
Try using "!":
send "mput /directory_name/*.* \n"
!mv /directory_name/*.xml /directory_name/archive/ 

send "bye\n" 

Open in new window

PS: "lmove" is not valid for many *nix implementations.
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 84

Expert Comment

by:ozo
ID: 40411231
If you meant
system "mv /directory_name/*.xml /directory_name/archive/"
you might want to do it after sftp is finished uploading them
0
 
LVL 1

Author Comment

by:Peter Kuczynski
ID: 40411252
I am now getting >>>> invalid command name "!mv"
                                        while executing

send "mput /directory_name/*.* \n"
!mv /directory_name/*.xml /directory_name/archive/

send "bye\n"

>you might want to do it after sftp is finished uploading them
                         I would like to do all this within one script if possible.
0
 
LVL 1

Author Comment

by:Peter Kuczynski
ID: 40411289
To recap my entire script plus the mv additions I need to make the files move which still I cannot get to move
I ran the mv commnd by itself and its correct, just not with the script.

#!/usr/bin/expect
spawn /usr/bin/sftp username@123.456.789.123
#sleep 5
expect "sftp>"
send "mput /directory1/*.* \n"
!mv /directory1/*.xml /directory1/archive/
expect "sftp>"
send "mput /directory2/*.* \n"
!mv /directory2/*.xml /directory2/archive/
expect "sftp>"
send "mput /directory3/*.* \n"
!mv /directory3/*.xml /directory3/archive/
expect "sftp>"
send "mput /directory4/*.* \n"
!mv /directory4/*.xml /directory4/archive/
expect "sftp>"
send "mput /directory5/*.* \n"
!mv /directory5/*.xml /directory5/archive/
expect "sftp>"
send "mput /directory6/*.* \n"
!mv /directory6/*.xml /directory6/archive/
send "bye\n"
0
 
LVL 84

Expert Comment

by:ozo
ID: 40411295
Did you try system?
0
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 40411326
Mmmm, works for me in AIX. Perhaps its not available in Linux.
Then you may want to follow Peters suggestion to do it after sftp is finished uploading.
0
 
LVL 1

Author Comment

by:Peter Kuczynski
ID: 40411422
This is interesting, I pasted the !mv command at the sftp prompt and it works!
So why would it not work in the script?

I'm doing this on RHEL 6.5
So if all fails, perform the mv commands in a seperate script?
0
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 40411450
Maybe you need to send it?:

send "!mv /directory5/*.xml /directory5/archive/\n"
0
 
LVL 1

Author Comment

by:Peter Kuczynski
ID: 40411459
runs with no errors but dosnt move the file
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 40411481
Did you try
system "mv /directory_name/*.xml /directory_name/archive/"
0
 
LVL 1

Author Comment

by:Peter Kuczynski
ID: 40411485
OMG it worked!
0
 
LVL 1

Author Comment

by:Peter Kuczynski
ID: 40411497
finished product.....

#!/usr/bin/expect
spawn /usr/bin/sftp username@123.456.789.123
#sleep 5
expect "sftp>"
send "mput /directory1/*.* \n"
system "!mv /directory1/*.xml /directory1/archive/ \n"
expect "sftp>"
send "mput /directory2/*.* \n"
system "!mv /directory2/*.xml /directory2/archive/ \n"
expect "sftp>"
send "mput /directory3/*.* \n"
system "!mv /directory3/*.xml /directory3/archive/ \n"
expect "sftp>"
send "mput /directory4/*.* \n"
system "!mv /directory4/*.xml /directory4/archive/ \n"
expect "sftp>"
send "mput /directory5/*.* \n"
system "!mv /directory5/*.xml /directory5/archive/ \n"
expect "sftp>"
send "mput /directory6/*.* \n"
system "!mv /directory6/*.xml /directory6/archive/ \n"
send "bye\n"
0
 
LVL 1

Author Comment

by:Peter Kuczynski
ID: 40411581
it works but it does not allow to execute multiple system commands embedded after a send command
meaning, once the first system command runs it does not process the next system command
it does appear to work if you group all the system command at the end.
Another issue is, if there is no file in a directory to move, the script stops.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 40412001
sftp is better for interactive use.   scp is much better in a script

#!/bin/bash
for dir in /directory?
do
  scp $dir/*.* username@123.456.789.123:
  mv $dir/*.xml $dir/archive
done

Open in new window

0
 
LVL 1

Author Comment

by:Peter Kuczynski
ID: 40415852
Here is what I finally wound up with, it runs fast.
Now I just need to add successful logging for files that send and ignore directories with no files



#!/bin/sh
sftp username@123.456.789.123
sleep 5
lcd /directory1
mput *.xml
!mv //directory1/*.xml /directory1/archive/

lcd /directory2
mput *.xml
!mv /directory2/*.xml /directory2/archive/

lcd /directory3
mput *.xml
!mv /directory3/*.xml /directory3/archive/

quit
EOF
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question