如何在 SFTP 执行结束时执行 shell 脚本?
How can I execute a shell script at the end of SFTP execution?
我有一个 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."
我没有看到 "Done." 打印在控制台上。有什么线索吗?
heredocs 要求出现在 <<
旁边的内容应该是
正是为了结束 heredoc 而制作的。
在您的情况下缩进引入的制表符(或之前可能有空格)heredoc delim END_SCRIPT
以便它不再被视为分隔符。
所以 sftp
命令继续寻找它从未找到的分隔符。
格式如下。
command <<HEREDOC_DELIM
HEREDOC_DELIM
# Note that nothing should be put in front of HEREDOC_DELIM - no indents.
问题是由于缩进引起的。有两种可能的解决方案
- 要么去掉缩进(至少
END_SCRIPT
行),
- 或者确保缩进是通过制表符而不是空格,并使用
<<-
而不是 <<
重定向运算符。
您还必须确保 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.
我有一个 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."
我没有看到 "Done." 打印在控制台上。有什么线索吗?
heredocs 要求出现在 <<
旁边的内容应该是
正是为了结束 heredoc 而制作的。
在您的情况下缩进引入的制表符(或之前可能有空格)heredoc delim END_SCRIPT
以便它不再被视为分隔符。
所以 sftp
命令继续寻找它从未找到的分隔符。
格式如下。
command <<HEREDOC_DELIM
HEREDOC_DELIM
# Note that nothing should be put in front of HEREDOC_DELIM - no indents.
问题是由于缩进引起的。有两种可能的解决方案
- 要么去掉缩进(至少
END_SCRIPT
行), - 或者确保缩进是通过制表符而不是空格,并使用
<<-
而不是<<
重定向运算符。
您还必须确保 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.