在C Shell中将stderr重定向到stdout [英] Redirect stderr to stdout in C shell

查看:272
本文介绍了在C Shell中将stderr重定向到stdout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在 csh 中运行以下命令时,我什么也没得到,但在 bash 中有效。
csh 中是否可以将标准错误重定向到标准输出?

When I run the following command in csh, I got nothing, but it works in bash. Is there any equivalent in csh which can redirect the standard error to standard out?

somecommand 2>&1


推荐答案

csh Shell从未以其在重定向过程中操纵文件句柄的广泛能力而闻名。

The csh shell has never been known for its extensive ability to manipulate file handles in the redirection process.

您可以使用以下命令将标准输出和错误重定向到文件:

You can redirect both standard output and error to a file with:

xxx >& filename

但这不是您想要的,将标准错误重定向到 current 标准输出。

but that's not quite what you were after, redirecting standard error to the current standard output.

但是,如果基础操作系统在文件系统中公开了进程的标准输出(就像Linux在 / dev / stdout 中所做的那样,您可以按如下方式使用该方法:

However, if your underlying operating system exposes the standard output of a process in the file system (as Linux does with /dev/stdout), you can use that method as follows:

xxx >& /dev/stdout

这将同时强制标准输出标准错误转到与当前标准输出相同的位置,有效地使用 bash 重定向, 2>& 1

This will force both standard output and standard error to go to the same place as the current standard output, effectively what you have with the bash redirection, 2>&1.

请记住,这不是 csh 功能。如果您运行的操作系统不会将标准输出显示为文件,则不能使用此方法。

Just keep in mind this isn't a csh feature. If you run on an operating system that doesn't expose standard output as a file, you can't use this method.

但是,还有另一种方法。如果使用 |& 发送到管道,则可以将两个流合并为一个,那么您要做的就是找到管道将其标准输入写入其标准输出的组件。如果您不知道这样的事情, cat 就是这样,如果您不给它任何参数的话。因此,您可以在以下特定案例中实现目标:

However, there is another method. You can combine the two streams into one if you send it to a pipeline with |&, then all you need to do is find a pipeline component that writes its standard input to its standard output. In case you're unaware of such a thing, that's exactly what cat does if you don't give it any arguments. Hence, you can achieve your ends in this specific case with:

xxx |& cat






当然,也没有什么可以阻止您运行 bash (假设它在系统上的某个位置) code>脚本为您提供增加的功能。然后,对于更复杂的 csh 可能难以解决的情况,您可以使用该shell的丰富重定向。


Of course, there's also nothing stopping you from running bash (assuming it's on the system somewhere) within a csh script to give you the added capabilities. Then you can use the rich redirections of that shell for the more complex cases where csh may struggle.

让我们探索一下这更详细。首先,创建一个可执行文件 echo_err ,它将向 stderr 中写入一个字符串:

Let's explore this in more detail. First, create an executable echo_err that will write a string to stderr:

#include <stdio.h>
int main (int argc, char *argv[]) {
    fprintf (stderr, "stderr (%s)\n", (argc > 1) ? argv[1] : "?");
    return 0;
}

然后使用控制脚本 test.csh 会显示它的作用:

Then a control script test.csh which will show it in action:

#!/usr/bin/csh

ps -ef ; echo ; echo $$ ; echo

echo 'stdout (csh)'
./echo_err csh

bash -c "( echo 'stdout (bash)' ; ./echo_err bash ) 2>&1"

echo PID和 ps 的用法很简单,因此您可以确保运行此脚本的 csh 。当您使用以下命令运行此脚本时:

The echo of the PID and ps are simply so you can ensure it's csh running this script. When you run this script with:

./test.csh >test.out 2>test.err

(初始重定向由 bash 设置 csh 开始运行脚本之前),并检查 out / err 文件,您会看到:

(the initial redirection is set up by bash before csh starts running the script), and examine the out/err files, you see:

test.out:
    UID     PID    PPID  TTY        STIME     COMMAND
    pax    5708    5364  cons0      11:31:14  /usr/bin/ps
    pax    5364    7364  cons0      11:31:13  /usr/bin/tcsh
    pax    7364       1  cons0      10:44:30  /usr/bin/bash

    5364

    stdout (csh)
    stdout (bash)
    stderr (bash)

test.err:
    stderr (csh)

您可以看到 test.csh 进程在C shell中运行,从那里调用 bash 可以为您提供完整的 bash 重定向的力量。

You can see there that the test.csh process is running in the C shell, and that calling bash from within there gives you the full bash power of redirection.

2>&a mp; 1 bash 命令中,可以很容易地将标准错误重定向到 current 标准输出(根据需要)无需事先知道标准输出的当前位置。

The 2>&1 in the bash command quite easily lets you redirect standard error to the current standard output (as desired) without prior knowledge of where standard output is currently going.

这篇关于在C Shell中将stderr重定向到stdout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆