gpt4 book ai didi

bash - 如何在 SFTP 执行结束时执行 shell 脚本?

转载 作者:行者123 更新时间:2023-11-29 09:42:09 24 4
gpt4 key购买 nike

我有一个 shell 脚本,我在其中执行 SFTP 并将我的文件传输到远程主机。

我可以将文件传输到远程主机,但是我的父shell脚本代码,一旦完成SFTP,就无法执行。

#!/bin/bash
set -e
echo "Starting SFTP..."
cd "/Users/myuser/myfolder"
sftp -o "StrictHostKeyChecking no" admin@myserver.myorg.com << END_SCRIPT
cd /some/folder/on/remote/host
put -r .
bye
END_SCRIPT
echo "Done."

我没有看到“完成”。在控制台上打印。有什么线索吗?

最佳答案

问题是由于缩进。有两种可能的解决方案

  1. 要么去掉缩进(至少对于 END_SCRIPT 行),
  2. 或者确保缩进是通过制表符而不是空格,并使用 <<-而不是 <<重定向运算符。

您还必须确保 END_SCRIPT 之后没有空格.

Here Documents

This type of redirection instructs the shell to read input from the current source until a line containing only delimiter (with no trailing blanks) is seen. All of the lines read up to that point are then used as the standard input for a command.

The format of here-documents is:

      <<[-]word
here-document
delimiter

No parameter and variable expansion, command substitution, arithmetic expansion, or pathname expansion is performed on word. If any characters in word are quoted, the delimiter is the result of quote removal on word, and the lines in the here-document are not expanded. If word is unquoted, all lines of the here-document are subjected to parameter expansion, command substitution, and arithmetic expansion, the character sequence \<newline> is ignored, and \ must be used to quote the characters \, $, and `.

If the redirection operator is <<-, then all leading tab characters are stripped from input lines and the line containing delimiter. This allows here-documents within shell scripts to be indented in a natural fashion.

关于bash - 如何在 SFTP 执行结束时执行 shell 脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48224253/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com