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

标签 bash shell ssh sh sftp

我有一个 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/

相关文章:

linux - 如何在使用 tcpdump 的 bash 脚本中处理 SIGKILL

linux - 只提取 url 段而不是 shell 脚本中的以下正则表达式?

linux - sed 的不同选项 - 是否获取重复行

linux - 为什么 sed 不接受 shell 变量?

Java SSH Ganymed - 尝试使用用户名和密码连接时出现随机错误

java - 在 Java 连接到主机后如何读取 SSH key 签名对横幅(用于生成 SSH 密码)?

ubuntu - 无法通过端口 443 连接到 ssh

python3 telnet read_all() 不起作用

bash - 使用 BASH 查找内部 IP 地址

bash - 从 bash 文件中读取星号字符 (*)