是否有一个标准的Bash工具,它像echo一样输出到stderr而不是stdout?
我知道我能做到,echo foo 1>&2但它有点难看,而且我怀疑,容易出错(例如,当事情发生变化时更容易被编辑错误).
Mar*_*lio 1321
这个问题很旧,但你可以做到这一点,这有助于阅读:
>&2 echo "error"
Run Code Online (Sandbox Code Playgroud)
运算符>&2字面意思是将文件描述符1(>&2)的地址重定向到该>&2命令1的文件描述符2()的地址.根据您想要了解它的深度,请阅读:http://wiki.bash-hackers.org/howto/redirection_tutorial
为避免与其他重定向的交互,请使用subshell
>&2 echo "error"
Run Code Online (Sandbox Code Playgroud)
1将>&2文件描述符#2复制到文件描述符#1.因此,在执行此重定向之后,两个文件描述符将引用相同的文件:一个文件描述符#2 最初是指.
Jam*_*oth 403
你可以定义一个函数:
echoerr() { echo "$@" 1>&2; }
echoerr hello world
Run Code Online (Sandbox Code Playgroud)
这比脚本更快,没有依赖性.
Camilo Martin的bash特定建议使用"here string"并将打印传递给它的任何内容,包括echo通常会吞下的参数(-n):
echoerr() { cat <<< "$@" 1>&2; }
Run Code Online (Sandbox Code Playgroud)
格伦杰克曼的解决方案也避免了吞咽问题:
echoerr() { printf "%s\n" "$*" >&2; }
Run Code Online (Sandbox Code Playgroud)
Bra*_*des 235
由于1是标准输出,因此您不必在输出重定向之前明确地命名它,>而是可以简单地键入:
echo This message goes to stderr >&2
由于您似乎担心1>&2难以可靠地打字,因此消除冗余1可能会给您带来轻微的鼓励!
小智 53
另外一个选项
echo foo >>/dev/stderr
Run Code Online (Sandbox Code Playgroud)
Mat*_*hen 31
不,这是执行此操作的标准方法.它不应该导致错误.
Grz*_*ywo 15
如果您不介意将消息记录到syslog中,则not_so_ugly方式为:
logger -s $msg
Run Code Online (Sandbox Code Playgroud)
-s选项表示:"将消息输出到标准错误以及系统日志."
小智 12
这是一个简单的STDERR函数,它将管道输入重定向到STDERR.
#!/bin/bash
# *************************************************************
# This function redirect the pipe input to STDERR.
#
# @param stream
# @return string
#
function STDERR () {
cat - 1>&2
}
# remove the directory /bubu
if rm /bubu 2>/dev/null; then
echo "Bubu is gone."
else
echo "Has anyone seen Bubu?" | STDERR
fi
# run the bubu.sh and redirect you output
tux@earth:~$ ./bubu.sh >/tmp/bubu.log 2>/tmp/bubu.err
Run Code Online (Sandbox Code Playgroud)
Don't use cat as some are mentioned here. cat is a program
while echo and printf are bash (shell) builtins. Launching a program or an other script (also mentioned above) means create an new process with all it's costs. Using builtins, writing functions are quite cheap, because there is no need to create (execute) a process (-environment).
The opner asks "is there any standard tool to output (pipe) to stderr", the schort answer is : NO ... why? ... rediredcting pipes is an elemantary concept in systems like unix (Linux...) and bash (sh) builds up on these concepts.
I agree with the opener that redirecting with notations like this: &2>1 is not very pleasant for modern programmers, but that's bash. Bash was not intended to write huge and robust programs, it is intended to help the admins to get there work with less keypresses ;-)
And at least, you can place the redirection anywhere in the line:
$ echo This message >&2 goes to stderr
This message goes to stderr
Run Code Online (Sandbox Code Playgroud)
注意:我正在回答帖子 - 而不是误导/模糊的"输出到stderr的回声"问题(已经由OP回答).
使用函数来显示意图并获得所需的实现.例如
#!/bin/bash
[ -x error_handling ] && . error_handling
filename="foobar.txt"
config_error $filename "invalid value!"
output_xml_error "No such account"
debug_output "Skipping cache"
log_error "Timeout downloading archive"
notify_admin "Out of disk space!"
fatal "failed to open logger!"
Run Code Online (Sandbox Code Playgroud)
并error_handling存在:
ADMIN_EMAIL=root@localhost
config_error() { filename="$1"; shift; echo "Config error in $filename: $*" 2>&1; }
output_xml_error() { echo "<error>$*</error>" 2>&1; }
debug_output() { [ "$DEBUG"=="1" ] && echo "DEBUG: $*"; }
log_error() { logger -s "$*"; }
fatal() { which logger >/dev/null && logger -s "FATAL: $*" || echo "FATAL: $*"; exit 100; }
notify_admin() { echo "$*" | mail -s "Error from script" "$ADMIN_EMAIL"; }
Run Code Online (Sandbox Code Playgroud)
处理OP问题的原因:
其他原因:
这已经得到了很好的回答.仅供记录:
echo "my errz" > /proc/self/fd/2
将有效输出到stderr.说明:/proc/self是当前进程的链接,并/proc/self/fd保存进程打开的文件描述符.然后,0,1,和2代表stdin,stdout并stderr分别.
我发现它更具可读性.这也适用于大多数Linux分发:
echo "my errz" > /dev/stderr
使它更具可读性.
制作脚本
#!/bin/sh
echo $* 1>&2
Run Code Online (Sandbox Code Playgroud)
那将是你的工具。
或者,如果您不想在单独的文件中包含脚本,则创建一个函数。
read 是一个shell内置命令,打印到stderr,可以像echo一样使用而不执行重定向技巧:
read -t 0.1 -p "This will be sent to stderr"
Run Code Online (Sandbox Code Playgroud)
这-t 0.1是一个禁用读取主要功能的超时,将一行stdin存储到变量中.
我最近偶然发现的另一个选择是:
{
echo "First error line"
echo "Second error line"
echo "Third error line"
} >&2
Run Code Online (Sandbox Code Playgroud)
这仅使用Bash内置函数,同时使多行错误输出的出错率降低(因为您不必记住要添加&>2到每一行)。
James Roth和Glenn Jackman建议的组合解决方案
echoerr() { printf "\e[31;1m%s\e[0m\n" "$*" >&2; }
# if somehow \e is not working on your terminal, use \u001b instead
# echoerr() { printf "\u001b[31;1m%s\u001b[0m\n" "$*" >&2; }
echoerr "This error message should be RED"
Run Code Online (Sandbox Code Playgroud)