【发布时间】:2020-04-03 13:01:05
【问题描述】:
我的主要问题是我无法从 sftp 下载最新文件。 另外,我只有下载文件的权限。 文件名示例:transaction_20200403060011_5e86xxxxxx.08595559.csv。 下载所有文件:
#!/usr/bin/expect
spawn sftp -i /home/ubuntu/sc_sftp.txt xxx@xxx.com
expect "password:"
send "xxx\n"
expect "sftp>"
send "get *.csv\n"
expect "sftp>"
send "exit\n"
interact
我只需要最新的文件,所以我尝试将文件名保存到 .txt 文件并读取我需要的文件名。它适用于 bash 命令。不幸的是,由于我使用 spawn 和 sftp 命令,我无法加载文件名。我的脚本,这不起作用:
#!/usr/bin/expect
spawn sftp -i /home/ubuntu/sc_sftp.txt xxx@xxx.com
expect "password:"
send "xxx\n"
expect "sftp>"
log_file -noappend RemoteFileList.txt
send "ls -1t\n"
expect "sftp>"
log_file
send "!sed -i '' '/ls -1/d' ./RemoteFileList.txt\n"
expect "sftp>"
send "!sed -i '' '/sftp>/d' ./RemoteFileList.txt\n"
expect "sftp>"
send "bye\n"
interact
set $file `(head -2 RemoteFileList.txt | tail -1)`
spawn sftp -i /home/ubuntu/sc_sftp.txt xxx@xxx.com
expect "password:"
send "xxx\n"
expect "sftp>"
send "get $file\n"
interact
和错误:
can't read "file": no such variable
while executing
"set $file `(head -2 RemoteFileList.txt | tail -1)`"
(file "./s.sh" line 16)
当我添加了这些命令时:
send "ls -1t\n"
expect -re "(.+)\r\nsftp>"
send " $expect_out(0,string)"
expect "sftp>"
send "exit\n"
interact
我有:
sftp> ls -1t
file1.csv
file2.csv
file3.csv
sftp> ls -1t
file1.csv
file2.csv
file3.csv
sftp>
sftp> file1.csv
Invalid command.
sftp>
sftp> file2.csv
sftp>
sftp> file3.csv
sftp>
sftp> sftp>exit
Invalid command.
有人可以帮我解决这个问题吗?
【问题讨论】:
-
Tcl 也是一种通用编程语言。您可以将它用于所有字符串操作。