bash - SFTP bash shell脚本将文件从源复制到目标

标签 bash shell sftp copying

我创建了一个脚本来将本地文件复制到远程文件夹。该脚本在 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/

相关文章:

bash - 如何获取所有可用 shell 命令的列表

c# - 是否可以更改用户 SFTP 密码 (renci.sshnet)

ubuntu - SFTP 文件上传非常慢并且在 Ubuntu 上停滞不前

bash - 以编程方式在 Heroku 中运行 bash 脚本文件

bash - 使用 grep 在输出中构造字符串

shell - 使用 Linux Mailx 发送简单的消息正文 + 文件附件

c - 如何从 Unix shell 脚本调用 C 函数?

bash - 在shell脚本中打印C程序的输出

linux - 日期和条件 bash 命令的奇怪但非常简单的问题

python - Python 中的递归 SFTP listdir?