sftp script quotes

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"
LVL 1
petersystems engineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
What was the command intended to do?
Did you want to send it to the sftp process?
0
petersystems engineerAuthor Commented:
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
MikeOM_DBACommented:
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
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

ozoCommented:
If you meant
system "mv /directory_name/*.xml /directory_name/archive/"
you might want to do it after sftp is finished uploading them
0
petersystems engineerAuthor Commented:
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
petersystems engineerAuthor Commented:
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
ozoCommented:
Did you try system?
0
MikeOM_DBACommented:
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
petersystems engineerAuthor Commented:
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
MikeOM_DBACommented:
Maybe you need to send it?:

send "!mv /directory5/*.xml /directory5/archive/\n"
0
petersystems engineerAuthor Commented:
runs with no errors but dosnt move the file
0
ozoCommented:
Did you try
system "mv /directory_name/*.xml /directory_name/archive/"
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
petersystems engineerAuthor Commented:
OMG it worked!
0
petersystems engineerAuthor Commented:
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
petersystems engineerAuthor Commented:
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
TintinCommented:
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
petersystems engineerAuthor Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.