Linux 如何将标准错误重定向到文件
在linux中,如果要将标准错误重定向到文件,可以执行以下操作:Linux 如何将标准错误重定向到文件,linux,bash,io-redirection,Linux,Bash,Io Redirection,在linux中,如果要将标准错误重定向到文件,可以执行以下操作: $ls -l /bin/usr 2> ls-error.txt 但当我尝试时: $foo= $echo ${foo:?"parameter is empty"} 2> ls-error.txt 终端中的结果是: bash: foo: parameter is empty 它不起作用! 有人能解释一下原因吗? 我以为${parameter:?word}会将word的值发送到标准错误。试试这个命令: ($echo${
$ls -l /bin/usr 2> ls-error.txt
但当我尝试时:
$foo=
$echo ${foo:?"parameter is empty"} 2> ls-error.txt
终端中的结果是:
bash: foo: parameter is empty
它不起作用!
有人能解释一下原因吗?
我以为${parameter:?word}
会将word的值发送到标准错误。试试这个命令:
($echo${foo:?“参数为空”})2>ls error.txtecho${foo:?“参数为空”}2>ls error.txt
重定向echo
的stderr
,但错误消息是由shell在扩展时生成的
${foo:?“参数为空”}
您可以通过重定向块(或子shell)来获得所需的结果,从而使shell的stderr包含在重定向中:
{ echo "${foo:?"parameter is empty"}"; } 2>ls-error.txt
如果要重定向sandard和error输出,并且在执行命令时仍要获取这些消息,可以使用tee命令:
$echo ${foo:?"parameter is empty"} |& tee -a ls-error.txt