bash 将字符串发送到标准输入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6541109/
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
Send string to stdin
提问by All Workers Are Essential
Is there a way to effectively do this in bash:
有没有办法在 bash 中有效地做到这一点:
/my/bash/script < echo 'This string will be sent to stdin.'
I'm aware that I could pipe the output from the echo such as this:
我知道我可以通过管道传输来自回声的输出,例如:
echo 'This string will be piped to stdin.' | /my/bash/script
回答by jm666
You can use one-line heredoc
您可以使用单行 heredoc
cat <<< "This is coming from the stdin"
the above is the same as
上面是一样的
cat <<EOF
This is coming from the stdin
EOF
or you can redirect output from a command, like
或者您可以重定向命令的输出,例如
diff <(ls /bin) <(ls /usr/bin)
or you can read as
或者你可以读为
while read line
do
echo =$line=
done < some_file
or simply
或者干脆
echo something | read param
回答by sehe
You were close
你很近
/my/bash/script <<< 'This string will be sent to stdin.'
For multiline input, here-docs are suited:
对于多行输入,here-docs 适合:
/my/bash/script <<STDIN -o other --options
line 1
line 2
STDIN
EditTo the comments:
编辑评论:
To achieve binary input, say
要实现二进制输入,说
xxd -r -p <<BINARY | iconv -f UCS-4BE -t UTF-8 | /my/bash/script
0000 79c1 0000 306f 0000 3061 0000 3093 0000 3077 0000 3093 0000 304b 0000 3093 0000 3077 0000 3093 0000 306a 0000 8a71 0000 306b 0000 30ca 0000 30f3 0000 30bb
0000 30f3 0000 30b9 0000 3092 0000 7ffb 0000 8a33 0000 3059 0000 308b 0000 3053 0000 3068 0000 304c 0000 3067 0000 304d 0000 000a
BINARY
If you substitute cat
for /my/bash/script
(or indeed drop the last pipe), this prints:
如果替换cat
为/my/bash/script
(或实际上下降了最后一个管道),这个打印:
私はちんぷんかんぷんな話にナンセンスを翻訳することができ
Or, if you wanted something a little more geeky:
或者,如果你想要一些更令人讨厌的东西:
0000000: 0000 0000 bef9 0e3c 59f8 8e3c 0a71 d63c .......<Y..<.q.<
0000010: c6f2 0e3d 3eaa 323d 3a5e 563d 090e 7a3d ...=>.2=:^V=..z=
0000020: 7bdc 8e3d 2aaf a03d b67e b23d c74a c43d {..=*..=.~.=.J.=
0000030: 0513 d63d 16d7 e73d a296 f93d a8a8 053e ...=...=...=...>
0000040: 6583 0e3e 5a5b 173e 5b30 203e 3d02 293e e..>Z[.>[0 >=.)>
0000050: d4d0 313e f39b 3a3e 6f63 433e 1c27 4c3e ..1>..:>ocC>.'L>
0000060: cde6 543e 59a2 5d3e 9259 663e 4d0c 6f3e ..T>Y.]>.Yf>M.o>
0000070: 60ba 773e cf31 803e ee83 843e 78d3 883e `.w>.1.>...>x..>
0000080: 5720 8d3e 766a 913e beb1 953e 1cf6 993e W .>vj.>...>...>
0000090: 7a37 9e3e c275 a23e dfb0 a63e bce8 aa3e z7.>.u.>...>...>
00000a0: 441d af3e 624e b33e 017c b73e 0ca6 bb3e D..>bN.>.|.>...>
00000b0: 6fcc bf3e 15ef c33e e90d c83e d728 cc3e o..>...>...>.(.>
00000c0: c93f d03e ac52 d43e 6c61 d83e f36b dc3e .?.>.R.>la.>.k.>
00000d0: 2f72 e03e 0a74 e43e 7171 e83e 506a ec3e /r.>.t.>qq.>Pj.>
00000e0: 945e f03e 274e f43e f738 f83e f11e fc3e .^.>'N.>.8.>...>
00000f0: 0000 003f 09ee 013f 89d9 033f 77c2 053f ...?...?...?w..?
0000100: caa8 073f 788c 093f 776d 0b3f be4b 0d3f ...?x..?wm.?.K.?
0000110: 4427 0f3f 0000 113f e8d5 123f f3a8 143f D'.?...?...?...?
0000120: 1879 163f 4e46 183f 8d10 1a3f cad7 1b3f .y.?NF.?...?...?
0000130: fe9b 1d3f 1f5d 1f3f 241b 213f 06d6 223f ...?.].?$.!?.."?
0000140: bb8d 243f 3a42 263f 7cf3 273f 78a1 293f ..$?:B&?|.'?x.)?
0000150: 254c 2b3f 7bf3 2c3f 7297 2e3f 0138 303f %L+?{.,?r..?.80?
0000160: 22d5 313f ca6e 333f ".1?.n3?
Which is the sines of the first 90 degrees in 4byte binary floats
哪个是 4byte 二进制浮点数中前 90 度的正弦值
回答by Rahul
You can also use read
like this
你也可以read
这样使用
echo "enter your name"
read name
echo $name
回答by tanius
Solution
解决方案
You want to (1) create stdout output in one process (like echo '…'
) and (2) redirect that output to stdin input of another process but (3) without the use of the bash pipe mechanism. Here's a solution that matches all three conditions:
您想 (1) 在一个进程中创建 stdout 输出(如echo '…'
)和 (2) 将该输出重定向到另一个进程的 stdin 输入,但 (3) 不使用 bash 管道机制。这是一个满足所有三个条件的解决方案:
/my/bash/script < <(echo 'This string will be sent to stdin.')
The <
is normal input redirection for stdin. The <(…)
is bash process substitution. Roughly it creates a /dev/fd/…
file with the output of the substituting command and passes that filename in place of the <(…)
, resulting here for example in script < /dev/fd/123
. For details, see this answer
这<
是标准输入的正常输入重定向。的<(…)
是bash进程替换。粗略地说,它/dev/fd/…
使用替换命令的输出创建一个文件,并传递该文件名代替 ,<(…)
例如在此处生成script < /dev/fd/123
. 有关详细信息,请参阅此答案
Comparison with other solutions
与其他解决方案的比较
A one-line heredoc sent to stdin
script <<< 'string'
only allows to send static strings, not the output of other commands.Process substitution alone, such as in
diff <(ls /bin) <(ls /usr/bin)
, does not send anything to stdin. Instead, process output is passed as the content of files created in the background, equivalent to for examplediff /dev/fd/10 /dev/fd/11
. In that command,diff
gets no input from stdin.
发送到 stdin 的单行 heredoc
script <<< 'string'
只允许发送静态字符串,而不是其他命令的输出。单独的进程替换,例如 in
diff <(ls /bin) <(ls /usr/bin)
,不会向 stdin 发送任何内容。相反,进程输出作为在后台创建的文件的内容传递,相当于例如diff /dev/fd/10 /dev/fd/11
. 在该命令中,diff
没有从 stdin 获取输入。
Use cases
用例
I like that, unlike the pipe mechanism, the < <(…)
mechanism allows to put the command first and all input after it, as is the standard for input from command line options.
我喜欢这一点,与管道机制不同,该< <(…)
机制允许将命令放在首位,然后将所有输入放在其后,这是从命令行选项输入的标准。
However, beyond commandline aesthetics, there are some cases where a pipe mechanism cannot be used. For example, when a certain command has to be provided as argument to another command, such as in this example with sshpass
.
但是,除了命令行美学之外,在某些情况下无法使用管道机制。例如,当某个命令必须作为另一个命令的参数提供时,例如在本例中使用sshpass
.
回答by Walf
cat | /my/bash/script
Enables one to type multiple lines into a program, without that input being saved in history, nor visible in ps
. Just press Ctrl + C when finished typing to end cat
.
允许一个人在程序中输入多行,而该输入不会保存在历史记录中,也不会在ps
. 完成输入后只需按 Ctrl + C 即可结束cat
。