我创建了一个脚本来将本地文件复制到远程文件夹。该脚本在 if
之外运行良好健康)状况。但是当我附在 if
条件 put
命令不起作用。它使用 SFTP 协议(protocol)登录到远程服务器,当存在时它显示错误:
put command not found
查看执行脚本后发生了什么:
Connected to 10.42.255.209.
sftp> bye
sftp.sh: line 23: put: command not found
请找到以下脚本。echo -e;
echo -e "This script is used to copy the files";
sleep 2;
localpath=/home/localpath/sftp
remotepath=/home/destination/sftp/
if [ -d $localpath ]
then
echo -e "Source Path found"
echo -e "Reading source path"
echo -e "Uploading the files"
sleep 2;
sftp username@10.42.255.209
put $localpath/* $remotepath
else
最佳答案
在这种简单的情况下,您可以使用 scp
代替sftp
并在命令行上指定要复制的文件:
scp $localpath/* username@10.42.255.209:/$remotepath/
但是如果你想发出 sftp 命令,那么 sftp 可以从它的标准输入中读取命令,所以你可以这样做:
echo "put $localpath/* $remotepath" | sftp username@10.42.255.209
或者您可以使用 here document将数据作为标准输入传递给 sftp,如果您想运行多个 sftp 命令,这可能会更容易:
sftp username@10.42.255.209 << EOF
put $localpath/fileA $remotepath/
put $localpath/fileB $remotepath/
EOF
最后,您可以将 sftp 命令放在一个单独的文件中,例如
sftp_commands.txt
。 ,并让 sftp 使用其 -b
执行这些命令旗帜: sftp -b ./sftp_commands.txt username@10.42.255.209
关于bash - SFTP bash shell脚本将文件从源复制到目标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53761918/