Solved

sftp script quotes

Posted on 2014-10-29
17
183 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

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…
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

751 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