我有一个程序输出到stdout,并希望在传递到文件时在Bash脚本中使该输出静音。
例如,运行程序将输出:
% myprogram
% WELCOME TO MY PROGRAM
% Done.
我希望以下脚本不向终端输出任何内容:
#!/bin/bash
myprogram > sample.s
我有一个程序输出到stdout,并希望在传递到文件时在Bash脚本中使该输出静音。
例如,运行程序将输出:
% myprogram
% WELCOME TO MY PROGRAM
% Done.
我希望以下脚本不向终端输出任何内容:
#!/bin/bash
myprogram > sample.s
Answers:
如果它也输出到stderr,则将其静音。您可以通过重定向文件描述符2来实现:
# Send stdout to out.log, stderr to err.log
myprogram > out.log 2> err.log
# Send both stdout and stderr to out.log
myprogram &> out.log      # New bash syntax
myprogram > out.log 2>&1  # Older sh syntax
# Log output, hide errors.
myprogram > out.log 2> /dev/null
&>比bash的4大很多
                    &>的简介?:o
                    >>&是在4.0中引入的。没有提及,&>但是CHANGES仅返回到2.0,所以我认为这已经在1.x中。
                    git blameon redir.c指向1998年提交,但仍在当前代码库中进行最早的提交。
                    这会将stderr(即描述符2)重定向到文件描述符1(即stdout)。
2>&1现在,当执行此操作时,您会将标准输出重定向到文件 sample.s
myprogram > sample.s合并这两个命令将导致将stderr和stdout都重定向到sample.s
myprogram > sample.s 2>&1/dev/null如果要完全使应用程序静默,请重定向到。
myprogram >/dev/null 2>&1myprogram >/dev/null 2>&1(注意/dev/null重定向之前的楔形和重定向顺序)。
                    myprogram &>/dev/null不够吗?
                    所有输出:
scriptname &>/dev/null便携式:
scriptname >/dev/null 2>&1便携式:
scriptname >/dev/null 2>/dev/null对于较新的bash(不可移植):
scriptname &>--并且&>是不可移植的bourne shell扩展。
                    echo moo 1>&-产生错误,因为文件描述符1已关闭:-bash: echo: write error: Bad file descriptor
                    如果您仍在努力寻找答案,特别是如果您为输出生成了文件,并且您希望使用明确的替代方法:
echo "hi" | grep "use this hack to hide the oputut :) "