如何将一命令标准错误输出到变量

sh/bash/dash/ksh/zsh等Shell脚本
回复
头像
自由建客
帖子: 13468
注册时间: 2008-07-30 23:21
系统: Debian stable AMD64

如何将一命令标准错误输出到变量

#1

帖子 自由建客 » 2015-09-24 19:08

代码: 全选

Second=$(dialog --nocancel --rangebox "$TextText" 2 40 1 30 3)
这样不对,因为 dialog 其实是用标准错误输出值的,怎么破?
头像
astolia
论坛版主
帖子: 6444
注册时间: 2008-09-18 13:11

Re: 如何将一命令标准错误输出到变量

#2

帖子 astolia » 2015-09-25 13:14

重定个向 2>&1
Russell_D_Lee
帖子: 4
注册时间: 2015-09-23 9:26

Re: 如何将一命令标准错误输出到变量

#3

帖子 Russell_D_Lee » 2015-09-25 14:06

由于使用$()可以把标准输出赋给变量,而我们可以重定向标准错误输出到标准输出,并且丢弃原标准输出的内容。
所以我只能想到:

代码: 全选

Second=$(dialog --nocancel --rangebox "$TextText" 2 40 1 30 3 2>&1 1>/dev/null)
不知道是否有更优雅的方法。
头像
susbarbatus
帖子: 2966
注册时间: 2010-04-10 16:14
系统: Arch Linux

Re: 如何将一命令标准错误输出到变量

#4

帖子 susbarbatus » 2015-09-25 16:11

Some widgets, e.g., checklist, will write text to dialog's output. Normally that is the standard error, but there are options for changing
this: "--output-fd", "--stderr" and "--stdout". No text is written if the Cancel button (or ESC) is pressed; dialog exits immediately in
that case.
沉迷将棋中……
头像
自由建客
帖子: 13468
注册时间: 2008-07-30 23:21
系统: Debian stable AMD64

Re: 如何将一命令标准错误输出到变量

#5

帖子 自由建客 » 2015-09-25 18:33

Russell_D_Lee 写了:由于使用$()可以把标准输出赋给变量,而我们可以重定向标准错误输出到标准输出,并且丢弃原标准输出的内容。
所以我只能想到:

代码: 全选

Second=$(dialog --nocancel --rangebox "$TextText" 2 40 1 30 3 2>&1 1>/dev/null)
不知道是否有更优雅的方法。
嗯,就是这样。
回复