简体   繁体   English

如何将标准输入重定向到 bash 中的文件

[英]How to redirect stdin to file in bash

Consider this very simple bash script:考虑这个非常简单的 bash 脚本:

#!/bin/bash
cat > /tmp/file

It redirects whatever you pipe into it to a file.它会将您通过管道输入的任何内容重定向到文件。 eg例如

echo "hello" | script.sh

and "hello" will be in the file /tmp/file. “你好”将在文件 /tmp/file.txt 中。 This works... but it seems like there should be a native bash way of doing this without using "cat".这有效......但似乎应该有一种不使用“cat”的本地bash方式来做到这一点。 But I can't figure it out.但我想不通。

NOTE:注意:

  1. It must be in a script.必须在脚本中。 I want the script to operate on the file contents afterwards.我希望脚本之后对文件内容进行操作。

  2. It must be in a file, the steps afterward in my case involve a tool that only reads from a file.必须在一个文件中,在我的例子中之后的步骤涉及一个只能从文件中读取的工具。

  3. I already have a pretty good way of doing this - its just that it seems like a hack.已经有一个很好的方法来做到这一点 - 只是它看起来像一个黑客。 Is there a native way?本土的方式吗? Like "/tmp/file < 0 " or "0> /tmp/file".像“/tmp/file < 0”或“0> /tmp/file”。 I thought bash would have a native syntax to do this...我认为 bash 会有一个本机语法来做到这一点......

You could simply do你可以简单地做

cp /dev/stdin  myfile.txt

Terminate your input with Ctrl+D or Ctrl+Z and, viola!使用 Ctrl+D 或 Ctrl+Z 终止您的输入,中提琴! You have your file created with text from the stdin.您已经使用标准输入中的文本创建了您的文件。

echo "$(</dev/stdin)" > /tmp/file

使用ENTER ctrl + d终止您的输入

I don't think there is a builtin that reads from stdin until EOF, but you can do this:我不认为有一个从 stdin 读取到 EOF 的内置函数,但你可以这样做:

#!/bin/bash
exec > /tmp/file
while IFS= read -r line; do
  printf '%s\n' "$line"
done

Another way of doing it using pure BASH:另一种使用纯 BASH 的方法:

#!/bin/bash

IFS= read -t 0.01 -r -d '' indata

[[ -n $indata ]] && printf "%s" "$indata" >/tmp/file

IFS= and -d '' causes all of stdin data to be read into a variable indata . IFS=-d ''导致所有标准输入数据被读入变量indata

Reason of using -t 0.01 : When this script is called with no input pipe then read will timeout after negligible 0.01 seconds delay.使用-t 0.01原因:当在没有输入管道的情况下调用此脚本时, read将在可忽略的0.01秒延迟后超时。 If there is any data available in input it will be read in indata variable and it will be redirected to >/tmp/file .如果输入中有任何可用数据,它将在indata变量中读取,并将被重定向到>/tmp/file

Another option: dd of=/tmp/myfile/txt另一种选择: dd of=/tmp/myfile/txt

Note: This is not a built-in, however, it might help other people looking for a simple solution.注意:不是内置的,但是,它可能会帮助其他人寻找简单的解决方案。

Why don't you just你为什么不只是

GENERATE INPUT | (
    # do whatever you like to the input here
)

But sometimes, especially when you want to complete the input first, then operate on the modified output, you should still use temporary files:但有时,尤其是想先完成输入,再对修改后的输出进行操作时,还是应该使用临时文件:

TMPFILE="/tmp/fileA-$$"
GENERATE INPUT | (
    # modify input
) > "$TMPFILE"
(
     # do something with the input from TMPFILE
) < "$TMPFILE"
rm "$TMPFILE"

If you don't want the program to end after reaching EOF, this might be helpful.如果您不希望程序在到达 EOF 后结束,这可能会有所帮助。

#!/bin/bash
exec < <(tail -F /tmp/a)
cat -

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM