如何将标准输入重定向到 bash 中的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36313562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to redirect stdin to file in bash
提问by Rafael Baptista
Consider this very simple bash script:
考虑这个非常简单的 bash 脚本:
#!/bin/bash
cat > /tmp/file
It redirects whatever you pipe into it to a file. e.g.
它会将您通过管道输入的任何内容重定向到文件。例如
echo "hello" | script.sh
and "hello" will be in the file /tmp/file. This works... but it seems like there should be a native bash way of doing this without using "cat". But I can't figure it out.
并且“hello”将在文件 /tmp/file.txt 中。这有效......但似乎应该有一种不使用“cat”的本地bash方式来做到这一点。但我想不通。
NOTE:
笔记:
It mustbe in a script. I want the script to operate on the file contents afterwards.
It mustbe in a file, the steps afterward in my case involve a tool that only reads from a file.
I alreadyhave a pretty good way of doing this - its just that it seems like a hack. Is there a nativeway? Like "/tmp/file < 0 " or "0> /tmp/file". I thought bash would have a native syntax to do this...
它必须在脚本中。我希望脚本之后对文件内容进行操作。
它必须在一个文件中,在我的例子中之后的步骤涉及一个只能从文件中读取的工具。
我已经有一个很好的方法来做到这一点 - 只是它看起来像一个黑客。有本土的方式吗?像“/tmp/file < 0”或“0> /tmp/file”。我认为 bash 会有一个本机语法来做到这一点......
采纳答案by fork2execve
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
回答by dee
You could simply do
你可以简单地做
cp /dev/stdin myfile.txt
Terminate your input with Ctrl+D or Ctrl+Z and, viola! You have your file created with text from the stdin.
使用 Ctrl+D 或 Ctrl+Z 终止您的输入,中提琴!您已经使用标准输入中的文本创建了您的文件。
回答by culmat
echo "$(</dev/stdin)" > /tmp/file
terminate your input with ENTERctrl+d
用ENTERctrl+终止你的输入d
回答by anubhava
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. If there is any data available in input it will be read in indata
variable and it will be redirected to >/tmp/file
.
使用原因-t 0.01
:当在没有输入管道的情况下调用此脚本时,read
将在可忽略的0.01
秒延迟后超时。如果输入中有任何可用数据,它将在indata
变量中读取并重定向到>/tmp/file
.
回答by ikrabbe
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"
回答by Speedstone
Another option: dd of=/tmp/myfile/txt
另外一个选项: dd of=/tmp/myfile/txt
Note:This is nota built-in, however, it might help other people looking for a simple solution.
注意:这不是内置的,但是,它可能会帮助其他人寻找简单的解决方案。