将文件内容放在stdin上的典型方法如下:
./command.sh < myfile
Run Code Online (Sandbox Code Playgroud)
这将myfile的所有内容放在stdin上,然后发送eof.我想在不添加EOF的情况下将内容放在stdin上.
对于各种交互式程序,我希望用一系列命令开始程序,但随后继续与程序交互.如果未发送eof,程序将等待更多输入,然后我可以交互式输入.
在bash中这可能吗?我目前的解决方案是将内容放在剪贴板上,然后粘贴它们.有没有更好的方法呢?
只需使用cat命令将文件与标准输入合并:
./command.sh < <(cat myfile -)
Run Code Online (Sandbox Code Playgroud)
或者
cat myfile - | ./command.sh
Run Code Online (Sandbox Code Playgroud)
cat命令cat代表con cat enate:
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.
...
Run Code Online (Sandbox Code Playgroud)
(请阅读精细手册;-)
你可以写
cat file1 file2 file3 ... fileN
Run Code Online (Sandbox Code Playgroud)
也
cat file1 - file2
cat - file1
cat file1 -
Run Code Online (Sandbox Code Playgroud)
根据您的需要...
我用它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')
Run Code Online (Sandbox Code Playgroud)
应该输出:
[Here is some text, followed by an empty line]
[]
[Then a string, on THIS line: Hello world]
Run Code Online (Sandbox Code Playgroud)
解决方案是使用fifo。
test -p /tmp/fifo || mkfifo /tmp/fifo
while true; do
read line < /tmp/fifo
echo "$line"
[[ $line == break ]] && $line
done
Run Code Online (Sandbox Code Playgroud)
并喂养fifo:
echo foobar > /tmp/fifo
Run Code Online (Sandbox Code Playgroud)
停止“听”:
echo break > /tmp/fifo
Run Code Online (Sandbox Code Playgroud)
看man mkfifo