SFTP bash shell 将文件从源复制到目标的脚本
SFTP bash shell script to copy the file from source to destination
我创建了一个脚本来将本地文件复制到远程文件夹。该脚本在 if
条件之外工作正常。但是当我包含在 if
条件中时,put
命令不起作用。它使用 SFTP 协议登录到远程服务器,当存在时显示错误:
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
这段代码对我有用
供参考阅读 https://help.oclc.org/Librarian_Toolbox/Exchange_files_with_OCLC/Upload_files_with_SFTP/40SFTP_commands?sl=en
uploadFileToMFT(){
sftp -P ${PORT_NO} ${HOST_NAME}@${HOST_ID} <<EOF
cd /mdm_dev05
put ${EXPORT_OUTPUT}'/'${ID}'/'${F_NAME}
quit
EOF
}
我使用这种格式得到了结果
HOST='xyz.abc.com'
USER='xyzasd'
REMOTEPATH='/var/www/data-csv/'
file_name='/tmp/sample.csv'
sftp $USER@$HOST <<EOF
cd /var/www/data-csv/
put $file_name
EOF
如果用户有密码,它会要求输入密码。否则这段代码工作正常。
我创建了一个脚本来将本地文件复制到远程文件夹。该脚本在 if
条件之外工作正常。但是当我包含在 if
条件中时,put
命令不起作用。它使用 SFTP 协议登录到远程服务器,当存在时显示错误:
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
这段代码对我有用 供参考阅读 https://help.oclc.org/Librarian_Toolbox/Exchange_files_with_OCLC/Upload_files_with_SFTP/40SFTP_commands?sl=en
uploadFileToMFT(){
sftp -P ${PORT_NO} ${HOST_NAME}@${HOST_ID} <<EOF
cd /mdm_dev05
put ${EXPORT_OUTPUT}'/'${ID}'/'${F_NAME}
quit
EOF
}
我使用这种格式得到了结果
HOST='xyz.abc.com'
USER='xyzasd'
REMOTEPATH='/var/www/data-csv/'
file_name='/tmp/sample.csv'
sftp $USER@$HOST <<EOF
cd /var/www/data-csv/
put $file_name
EOF
如果用户有密码,它会要求输入密码。否则这段代码工作正常。