我的程序中有这个代码
while(1){
// some code here
fprintf(stdout,"Output Log");
fprintf(stderr,"Error Log");
//some code here
}
Run Code Online (Sandbox Code Playgroud)
它只打印"错误日志".看起来我错过了两个fprintf之间的冲洗.因此我将"\n"附加到字符串"输出日志".工作得很好.但当我换掉两个fprintf时,无法理解这种奇怪的行为
while(1){
// some code here
fprintf(stderr,"Error Log\n");
fprintf(stdout,"Output Log");
//some code here
}
Run Code Online (Sandbox Code Playgroud)
尽管使用"\n"它只打印"错误日志".
我正在尝试编写一个bash脚本,从文件夹中删除重复的文件,只保留一个副本.该脚本如下:
#!/bin/sh
for f1 in `find ./ -name "*.txt"`
do
if test -f $f1
then
for f2 in `find ./ -name "*.txt"`
do
if [ -f $f2 ] && [ "$f1" != "$f2" ]
then
# if cmp $f1 $f2 &> /dev/null # DOES NOT WORK
if cmp $f1 $f2
then
rm $f2
echo "$f2 purged"
fi
fi
done
fi
done
Run Code Online (Sandbox Code Playgroud)
我想重定向输出和stderr以/dev/null避免将它们打印到屏幕..但使用注释语句此脚本不能按预期工作并删除所有文件,但第一个..
如果需要,我会提供更多信息.
谢谢
如果我像这样运行Bash脚本:
./script.sh 2>&1
Run Code Online (Sandbox Code Playgroud)
stderr将被重定向到stdout.
如果脚本在内部调用某个工具(例如ls)或生成一个新进程,这些子进程是否stderr也将它们重定向到stdout?
我试图从Perl库运行一个特定的子程序,该库在执行时打印大量的HTML代码.在做了一些研究之后,我发现可以使用select()方法暂时将print输出更改为STDERR,将其完全隐藏在前端,然后在库完成其工作后将其恢复到STDOUT.
很简单,但这对我不起作用.出于某种原因,有人在select()关键库中创建了一个与上述无关的方法,但是是一种与我们的数据库交互的方法.因此,每当我尝试使用时my $filehandler = select(STDERR);,我都会使用数据库方法,这会导致错误.我无法评论此方法或将其从该库中的导出方法中删除,而不会在其他脚本(包括我正在尝试运行的库)上造成太多麻烦.
我甚至不能使用HTML块注释来摆脱这种情况,因为库也会打印注释.有没有其他选择来运行正确的select()方法?或者任何其他替代方案可以暂时阻止HTML打印?
编辑:作为参考,我的Perl版本是为x86_64-linux-thread-multi构建的v5.10.1(*)
我需要编写类似于compiletest的东西.Compiletest通过调用rustc进程并捕获stdout/stderr来工作.我的项目还没有运行二进制文件,所以我需要另一种方法来创建子进程.
我不能使用频道或stdout,因为它不受我的代码控制.
The following code
<?php
exit("where");
?>
Run Code Online (Sandbox Code Playgroud)
prints the word where to stdout.
How to print exit() output to stderr?
我正在尝试使用with声明来抑制sys.stdout或sys.stderr单独使用。 我发现了一个不起作用的教程。我正在使用Python 3.6.4并且我认为该教程是Python 2.
我在 SO 上查了一下,发现了一些但应用程序不起作用或不适用于这种情况。
这不适用:Python subprocess supress stdout and stderr
无法使任何with语句起作用:
Suppress stdout / stderr print from Python functions
这适用于 fortran:在 Python 中重定向 FORTRAN(通过 F2PY 调用)输出
from contextlib import contextmanager
@contextmanager
def suppress_console(file=sys.stdout):
with open(os.devnull, "w") as devnull:
old_file = file
file = devnull
try:
yield
finally:
file = old_file
with suppress_console():
print(1, file=sys.stdout)
# 1
Run Code Online (Sandbox Code Playgroud) 我正在交互式shell中执行bash命令
./somescript.sh
它给出了输出
OS platform is: linux2
killall agent
agent: no process killed
Run Code Online (Sandbox Code Playgroud)
其中第三行来自stderr.
但是当我在子shell中执行时
var=$('./somescript.sh' 2>&1)
agent: no process killed
OS platform is: linux2
killall agent
Run Code Online (Sandbox Code Playgroud)
为什么代理人:现在第一行没有打印过程?如何使它们保持一致以对齐它们?
编辑:但是当我这样做时,
var=$('./somescript.sh' 1>&2)
我可以看到它在bash调试模式下以正确的顺序给出输出.但它没有存储在变量var中.
以下命令:
ls > . 2> error
Run Code Online (Sandbox Code Playgroud)
印刷:
bash: .: Is a directory
Run Code Online (Sandbox Code Playgroud)
到终端。但是由于我将 stderr 重定向到“错误”,我希望这一行被写入错误而不是终端。