【问题标题】:Another way to redirect output in Bash在 Bash 中重定向输出的另一种方法
【发布时间】:2016-07-31 10:51:06
【问题描述】:


在 bash 中,当我们要读取文件时,我们使用 cat 命令
cat file.txt
但如果我们不想使用空格,我们可以输入:
{cat,file.txt}
有没有办法在不使用符号 > 或 的情况下重定向输出 我的意思是有没有相当于这个命令:
cat file.txt > /dev/null
谢谢。

【问题讨论】:

  • 你为什么要关心空格?我真的无法想象你需要{cat,file.txt}的情况。
  • 您实际上想完成哪些您不能(或不想)使用>

标签: bash redirect output


【解决方案1】:

|tee(称为 pipe-T)- 将输出(与 > 相同)重定向到文件,但也会在标准输出上打印:

cat file.txt|tee outfile.txt

|tee -a: 将输出附加到文件(与 >> 相同)但也会在标准输出上打印:

cat file.txt|tee -a outfile.txt

【讨论】:

  • ... 或 | dd of=outfile.txt.
  • 说“在控制台上打印”是不准确的。它将数据写入指定文件以及标准输出。这可能是一个关联的 tty,但通常不是。 (几乎可以肯定它不是控制台。)不要将 stdout 与 tty 混为一谈。
【解决方案2】:

我不明白你为什么要这样做,但你可以这样做:

eval {cat,input} "$(echo _/dev/null | tr _ '\076')"

【讨论】:

  • 谢谢。这就是我一直在寻找的。​​span>
【解决方案3】:

除了tee,您还可以使用exec 来重定向输出

 exec 3>&1 # making file descriptor 3 to point to 1 where 1 is stdout
 exec 1>fileout #redirecting 1 ie stdout to a file
 {cat,file} # this goes to fileout & question requirement
 exec 1>&3 # restoring 1 to default
 {cat,38682813.c} # This will be printed at the stdout

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-10
    • 2014-11-23
    • 1970-01-01
    • 2013-06-17
    • 2014-11-17
    相关资源
    最近更新 更多