我有一些脚本使用的参数,他们工作很好,但我希望他们能够从stdin读取,从管道,例如,一个例子,假设这被称为读取:
- #!/bin/bash
- function read()
- {
- echo $*
- }
- read $*
现在这个工作与读“foo”“酒吧”,但我想使用它作为:
- echo "foo" | read
如何完成这个?
您可以使用<<<得到这个行为。读取<<<回声“文本”应该。 用readly测试(我不喜欢使用保留字):
- function readly()
- {
- echo $*
- echo "this was a test"
- }
- $ readly <<< echo "hello"
- hello
- this was a test
带管道,基于this answer to “Bash script,read values from stdin pipe”:
- $ echo "hello bye" | { read a; echo $a; echo "this was a test"; }
- hello bye
- this was a test