gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 14:05:10 26 4
gpt4 key购买 nike

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

sshpass -pABC123 sftp user@host.com  << !
cd data
put /path/file.txt
bye
!

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

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

我已经试过了:
SFTP_RESULT = (    
sshpass -pABC123 sftp user@host.com << !
cd data
put /path/file.txt
bye
!
)

我也尝试过:
sshpass -pABC123 sftp user@host.com  << ! >> /file.txt


sshpass -pABC123 sftp user@host.com >> /file.txt << ! 

所有这些简单地返回了我发送给远程服务器的命令。我看不到来自服务器的任何响应。当我使用上述任何命令从命令行运行脚本时,我会在屏幕上看到响应。

有没有人有什么建议?

更新

我接受了拉斯·科特霍夫(Lars Kotthoff)的回答,尽管它并不完美,但基于他的回答以及我们在对他的回答的评论中的讨论,我弄清楚了。这是我所做的:

首先,我将sftp命令移至名为“sftp_commands”的外部文件
echo $(cat sftp_commands | sshpass -pABC123 sftp user@host.com 2>&1) >> sftp.log

由于某些原因,它起作用。

最佳答案

重定向工作:

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

关于bash - 在bash中输入多行重定向后如何重定向输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9776486/

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