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

问题描述

我在我的机器上运行tcsh / csh shell。最近我意识到我的stderr文件重定向不起作用。

请查找终端日志如下:

 > echo bc>>& log 
> cat log
b $
> echo $ a b c>>&日志
a:未定义的变量。
>猫日志
bc

我从来没有遇到这样的问题,确定如何调试或排除故障。
请指教!

改变重定向的方法是使用tee。
当我使用&或>>& ,我完全阻止任何输出显示在终端上。
有两种方法可以做到这一点,即:对于每个语句
$ b $ a)将stdout + stderr直接导入到一个文件中。



b)在终端上显示stdout + stderr

我需要确认是否可以使用类似下面的内容来满足我的要求

 > csh ./script_name | tee -a log 

在我的情况下,它只将stdout指向日志文件并阻止stderr。

解决方案

我非常确定 $ a 当前的shell,之前的重定向是为 echo 子shell设置的?



它似乎得到了支持,因为 echo 实际上并不是真的写入文件,你可以看到如果你删除了 log 文件在第二次尝试之前:

  [pax〜] $ echo bc>>&日志

[pax〜] $ cat log
bc
$ b $ [pax〜] $ rm log

[pax〜] $ echo $ abc>> &安培;日志
a:未定义的变量。

[pax〜] $ cat log
cat:log:没有这样的文件或目录

还有一个事实,如果你在一个显式的子shell中运行 echo 并为子shell重定向而不是<$ c $你可以得到你想要的结果:

$ $ $ $ $ $ $ $ $ $($ $ $ )>> &安培; log

[pax〜] $ cat log
b
a:未定义的变量。

实际上,即使不使用有效的命令也可以看到:

  [pax〜] $ rm log 

[pax〜] $ $ xyzzy
xyzzy:未定义的变量。

[pax〜] $ $ xyzzy>>& log
xyzzy:未定义的变量。

[pax〜] $ cat log
cat:log:没有这样的文件或目录

原因是当前shell正在抱怨试图评估 $ xyzzy 。这是有道理的。用命令:

  someCmd $ HOME 

someCmd 可执行文件将永远不会看到文本 $ HOME ,它实际上被替换为值。而且这个替换是在shell之前完成的 someCmd 甚至被设置(包括修改标准输入或输出流)。

I am running tcsh/csh shell on my machine. lately i have realized my stderr file redirection is not working.

Please find the terminal logs as below:

>echo b c >>& log
>cat log
b c
>echo $a b c >>& log
a: Undefined variable.
>cat log
b c

I have never faced such issues, hence not sure how to debug or troubleshoot.Please advice!

An alterate method of redirection is using tee.While I use >& or >>& , i am altogether blocking any output to be displayed on terminal.Is there a way I can do both, that is: for each statement

a) direct stdout+stderr to a file.

b) display stdout+stderr on terminal

I need to confirm if i can use something like this(below) to meet my requirements

>csh ./script_name | tee -a log

In my case it only directs stdout to log file and blocks stderr.

解决方案

I'm pretty certain that the evaluation of $a is being done by the current shell, before the redirection is set up for the echo subshell?

It seems to be supported by the fact that the echo isn't actually writing to the file at all, which you could see if you deleted the log file before the second attempt:

[pax ~]$ echo b c >>& log

[pax ~]$ cat log
b c

[pax ~]$ rm log

[pax ~]$ echo $a b c >> & log
a: Undefined variable.

[pax ~]$ cat log
cat: log: No such file or directory

And also by the fact that, if you run the echo in an explicit subshell with the redirection done for the subshell rather than the echo, you get your desired result:

[pax ~]$ ( echo $a b c ) >> & log

[pax ~]$ cat log
b c
a: Undefined variable.

In fact, you can see this without even using a valid command:

[pax ~]$ rm log

[pax ~]$ $xyzzy
xyzzy: Undefined variable.

[pax ~]$ $xyzzy >>&log
xyzzy: Undefined variable.

[pax ~]$ cat log
cat: log: No such file or directory

The reason for this is that the current shell is complaining about trying to evaluate $xyzzy. This makes sense. With the command:

someCmd $HOME

the someCmd executable will never see the literal $HOME, it's actually replaced with the value. And that replacement is done by the shell before someCmd is even set up (including having its standard input or output streams modified).

这篇关于stderr重定向方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 11:12