Bash脚本sftp仅将文件下载到同一目录脚本位于

Bash脚本sftp仅将文件下载到同一目录脚本位于

我有一个奇怪的问题。我正在尝试将文件从 ftp 服务器下载到与 bash 脚本运行的目录不同的目录。如果我手动执行,一切都会正常。我可以连接类型

get sampleFile.csv /path/to/directory/

尝试将其放入 bash 脚本中是行不通的。我得到与手动执行相同的输出

/ftp/sampleFile.csv 100%  384     0.4KB/s   00:01

但该文件不在目录中或我在服务器上可以找到的任何位置。我努力了

sftp ***** << EOF
get sampleFile.csv /path/to/directory/
quit
EOF

cd /path/to/directory/
sftp ***** << EOF
get sampleFile.csv
quit
EOF    

sftp ***** << EOF
lcd /path/to/directory
get sampleFile.csv
quit
EOF    

所有这些工作都是手动完成的。

唯一有效的是

sftp ***** << EOF
get sampleFile.csv
quit
EOF 

这会将文件放在 bash 脚本所在的同一目录中并显示出来。有什么想法吗?

答案1

在 sftp 脚本行lcd path/you/want/to/save/to之前添加。get

 ❯ sftp localhost
Connected to localhost.
# can't get sampleFile.csv because lpwd isn't what I think
sftp> get sampleFile.csv path/to/directory
File "/home/ryan/sampleFile.csv" not found.
sftp> lpwd
Local working directory: /home/ryan
# this will grab the file I want, but save it into `lpwd` again
sftp> get path/to/directory/sampleFile.csv
Fetching /home/ryan/path/to/directory/sampleFile.csv to sampleFile.csv
# pretend I want to save it locally in /tmp... so: lcd /tmp
sftp> lcd /tmp
sftp> get path/to/directory/sampleFile.csv
Fetching /home/ryan/path/to/directory/sampleFile.csv to sampleFile.csv

相关内容