在 stderr 上打印消息的 Bash 命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2643165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bash command that prints a message on stderr
提问by Salman A
I want to know if there is a built-in BASH command that prints some text on stderr, just like the echocommand that prints text on stdout. I don't want to use temporary io-redirection. I use a built-in command to generate an error on stderr such as ls --asdf(ls: unrecognized option '--asdf') but I want something neater.
我想知道是否有一个内置的BASH命令可以在stderr上打印一些文本,就像echo在stdout上打印文本的命令一样。我不想使用临时 io 重定向。我使用内置命令在 stderr 上生成错误,例如ls --asdf(ls: unrecognized option '--asdf') 但我想要更简洁的东西。
Edit ----
编辑 - -
Actually I am trying to demonstrate stderr/stdout redirection, and my example looks like:
实际上,我正在尝试演示 stderr/stdout 重定向,我的示例如下所示:
sh test.sh >test-out.txt 2>test-err.txt
For clarity, I want to keep the test.sh file as simple and clean as possible, this means avoiding >operator inside the file.
为清楚起见,我想让 test.sh 文件尽可能简单和干净,这意味着避免>在文件中使用运算符。
回答by Paused until further notice.
echo something >&2is the correct way to do what you want.
echo something >&2是做你想做的正确方法。
However...
然而...
This will create a little program that will echo its arguments to stderr:
这将创建一个小程序,将其参数回显到stderr:
gcc -o echoerr -x c - <<'EOF'
#include <stdio.h>
#include <stdlib.h>
void main(int argc, char **argv) {
int i;
for (i = 1; i < argc; i++) {
fprintf(stderr, "%s ", argv[i]);
}
fprintf(stderr, "\n");
exit(0);
}
EOF
You can use it like this:
你可以这样使用它:
$ ./echoerr this is an error message
this is an error message
$ ./echoerr this is an error message 2>error.out
$ cat error.out
this is an error message
回答by nic ferrier
No one has mentioned this but you can also do this:
没有人提到这一点,但你也可以这样做:
echo An error message > /dev/stderr
echo An error message > /dev/stderr
It's possibly more readable than >&2but I guess that depends who you are.
它可能比>&2但我想这取决于你是谁更易读。
回答by Jürgen H?tzel
No builtin, you could use:
没有内置,你可以使用:
function echo-err { echo "$@" >&2; }
回答by Chris Cooper
You could also make an alias.
你也可以做一个别名。
alias echoerr='echo >&2'
回答by user unknown
Just a sidenote: If you like to demonstrate bash, you should use bash, not sh:
只是一个旁注:如果你想演示 bash,你应该使用 bash,而不是 sh:
sh test.sh >test-out.txt 2>test-err.txt
bash test.sh >test-out.txt 2>test-err.txt
Even if sh is a link to bash on your system, it will check how it was called (arg0) and treat sh-calls like an invocation
即使 sh 是您系统上 bash 的链接,它也会检查它的调用方式(arg0)并将 sh 调用视为调用
bash --posix
which leads to sometimes subtile different behaviour. A common mistake, like shebangs #/bin/sh 'but it did work in the shell' (which was /bin/bash).
这有时会导致微妙的不同行为。一个常见的错误,比如 shebangs #/bin/sh '但它确实在 shell 中工作'(这是 /bin/bash)。
回答by Personman
In general, I think that there is not really a difference between 'temporary io-redirection' and the thing you want. stderr is a file descriptor like any other, and any builtin that does what you want would just be doing the same thing internally that you do by redirecting echo as Jurgen suggested above, namely, calling write() on it.
总的来说,我认为“临时 io 重定向”和您想要的东西之间并没有真正的区别。stderr 是一个与任何其他文件描述符一样的文件描述符,并且任何执行您想要的操作的内置程序都将在内部执行与您通过重定向回声(如 Jurgen 上面建议的那样)所做的相同的事情,即在其上调用 write()。

