gpt4 book ai didi

bash - 将文件内容放入标准输入而不发送eof

转载 作者:行者123 更新时间:2023-11-29 08:57:26 26 4
gpt4 key购买 nike

将文件内容放入标准输入的典型方法如下:

./command.sh < myfile

这会将 myfile 的所有内容放入标准输入,然后也发送 eof。我想在不添加 EOF 的情况下将内容放入标准输入。

对于各种交互式程序,我希望以一系列命令开始程序,然后继续与程序交互。如果未发送 eof,程序将等待更多输入,然后我可以交互输入。

这在 bash 中可行吗?我目前的解决方案是将内容扔到剪贴板上,然后粘贴它们。有更好的方法吗?

最佳答案

使用 cat 命令简单地将文件与 stdin 合并:

./command.sh < <(cat myfile -)

cat myfile - | ./command.sh

cat 命令

cat 代表concatenate:

man cat
NAME
cat - concatenate files and print on the standard output

SYNOPSIS
cat [OPTION]... [FILE]...

DESCRIPTION
Concatenate FILE(s), or standard input, to standard output.
...

(请R阅读TFM年度;-)

你可以写

cat file1 file2 file3 ... fileN

还有

cat file1 - file2

cat - file1

cat file1 -

根据您的需要...

复杂示例,使用heredocherestring 而不是文件

我使用 head -c -1 以便在 heredoc删除尾随 换行符sed 命令确实模拟任何命令处理逐行:

sed 's/.*/[&]/' < <(cat <(head -c -1 <<eof 
Here is some text, followed by an empty line

Then a string, on THIS line:
eof
) - <<<'Hello world')

应该输出:

[Here is some text, followed by an empty line]
[]
[Then a string, on THIS line: Hello world]

关于bash - 将文件内容放入标准输入而不发送eof,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14086738/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com