gpt4 book ai didi

linux - 如何捕获管道命令错误消息

转载 作者:太空宇宙 更新时间:2023-11-04 04:53:32 25 4
gpt4 key购买 nike

如果命令在任何管道中失败或者命令成功其输出在变量中,我将 try catch 错误消息。

k=$(ls | pl) 
pl: command not found

我也尝试过以下操作,但无法存储错误消息

$ k=$(ls | pl) > out 2>&1
pl: command not found
$ cat out

文件仍然是空的。有什么办法可以实现这一点吗?

最佳答案

只需像这样重定向:

$ erroe=`ls | pl 2>&1`
mayankp@mayank:~$ echo $erroe
pl: command not found

关于linux - 如何捕获管道命令错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52872089/

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