Link to home
Start Free TrialLog in
Avatar of peter
peterFlag for United States of America

asked on

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"
Avatar of ozo
ozo
Flag of United States of America image

What was the command intended to do?
Did you want to send it to the sftp process?
Avatar of peter

ASKER

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"
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.
If you meant
system "mv /directory_name/*.xml /directory_name/archive/"
you might want to do it after sftp is finished uploading them
Avatar of peter

ASKER

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.
Avatar of peter

ASKER

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"
Did you try system?
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.
Avatar of peter

ASKER

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?
Maybe you need to send it?:

send "!mv /directory5/*.xml /directory5/archive/\n"
Avatar of peter

ASKER

runs with no errors but dosnt move the file
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of peter

ASKER

OMG it worked!
Avatar of peter

ASKER

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"
Avatar of peter

ASKER

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.
Avatar of Tintin
Tintin

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

Avatar of peter

ASKER

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