如何在多行重定向后重定向输出以输入 bash?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9776486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 01:48:38  来源:igfitidea点击:

How to redirect output after a multiline redirect for input in bash?

bashsshcronsftp

提问by ethanpil

I have a bash script that uploads a file via SFTP, with a command like this:

我有一个通过 SFTP 上传文件的 bash 脚本,命令如下:

sshpass -pABC123 sftp [email protected]  << !
   cd data
   put /path/file.txt
   bye
! 

I get an email notification from my bash script when the process is complete. I would like to capture the actual output from this command (the responses from the server or errors from sshpass) into a variable or text file and include it in my email as well.

过程完成后,我会从我的 bash 脚本中收到一封电子邮件通知。我想将此命令的实际输出(来自服务器的响应或来自 sshpass 的错误)捕获到一个变量或文本文件中,并将其包含在我的电子邮件中。

What are my options to redirect the output. I know I can place commands into $() to capture their output, and I can use >> as well, but with the multiline input, I dont think these will work...

我有哪些重定向输出的选项。我知道我可以将命令放入 $() 以捕获它们的输出,我也可以使用 >> ,但是对于多行输入,我认为这些不会起作用......

I have tried this:

我试过这个:

SFTP_RESULT = (    
   sshpass -pABC123 sftp [email protected]  << !
       cd data
       put /path/file.txt
       bye
    ! 
)

And I have also tried:

我也试过:

sshpass -pABC123 sftp [email protected]  << ! >> /file.txt

and

sshpass -pABC123 sftp [email protected] >> /file.txt << ! 

All of these simply return back my commands which I am sending to the remote server. I dont see any of the responses from the server. When I run the script form the command line with any of the above, I see the responses on the screen.

所有这些都只是返回我发送到远程服务器的命令。我没有看到来自服务器的任何响应。当我使用上述任何一种方式从命令行运行脚本时,我会在屏幕上看到响应。

Does anyone have any suggestions?

有没有人有什么建议?

UPDATE

更新

I have accepted the answer from Lars Kotthoffeven though its not perfect, but based on his answer and our discussion in the comments to his answer I figured it out. This is what I did:

我已经接受了Lars Kotthoff的答案,尽管它并不完美,但根据他的回答和我们在对他的回答的评论中的讨论,我想通了。这就是我所做的:

First I moved the sftp commands to an external file called "sftp_commands"

首先,我将 sftp 命令移动到名为“sftp_commands”的外部文件

echo $(cat sftp_commands | sshpass -pABC123 sftp [email protected] 2>&1) >> sftp.log

For some reason that works.

出于某种原因,这是有效的。

采纳答案by Lars Kotthoff

Redirects work:

重定向工作:

$ cat << ! > /tmp/foo
> foo
> bar
> !
$ cat /tmp/foo 
foo
bar

回答by ghoti

Sounds to me like you want to use an expectscript. Expect should be available for your operating system and distribution.

在我看来,您想使用期望脚本。Expect 应该可用于您的操作系统和发行版。

Barring that, see if you can use scp, which is often available on servers that also have sftp enabled. It may be much easier just to:

除此之外,看看您是否可以使用scp,这通常在启用了 sftp 的服务器上可用。仅执行以下操作可能会容易得多:

scp /path/file.txt [email protected]:data/

Note that it's probably a better idea to set up SSH public keys rather than use sshpass.

请注意,设置 SSH 公钥可能比使用 sshpass 更好。