简体   繁体   中英

sftp via bash script in a cronjob: 'get' and 'rm' commands not found?

I'm trying to do something real, real simple: download a file from a sftp site at a set time. However, I'm getting errors like 'command: rm does not exist' or 'command get does not exist' which is garbage, because these commands obviously exist.

#!/usr/bin/expect
#remove current version of file
rm -f /home/user/downloads/data/newdata.zip

spawn sftp user@ftpsite.com
expect "password:"
send "PaSsWoRd\n"
expect "sftp>"
get /mycompany/myproject/data/newdata.zip /home/user/downloads/data
expect "sftp>"
send "exit\n"
interact

(base) root@ubuntu:~# ./shellscript.sh
invalid command name "rm"
    while executing
"rm -f /home/user/downloads/data/newdata.zip"

or without the removal

(base) root@ubuntu:~# sudo ./shellscript.sh
spawn sftp user@sftpsite.com
EFT Server Login - %DATE% %TIME% - Please enter valid credentials to continueEnter password: 
Connected to ftpsite.com
sftp> invalid command name "get"
    while executing
"get /mycompany/myproject/data/newdata.zip /home/user/downloads/data"

What gives? Why can't I get real error messages - I'm sure get and rm a perfectly fine commands and I can verify this by running them directly from the command line, where they work fine.

You are writing TCL code in an Expect script. You are not writing Bash in a shell script.

You have to send the commands you want to write to the sftp tool:

send "get /mycompany/myproject/data/newdata.zip /home/user/downloads/data\n"

And exec any commands you want to run non-interactively:

exec rm -f /home/user/downloads/data/newdata.zip

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM