[英]Read from a file and stdin in Bash
I would like to know if I can write a shell script that accepts two arguments simultaneously , one from a file and the another one from stdin.我想知道我是否可以编写一个同时接受两个参数的 shell 脚本,一个来自文件,另一个来自标准输入。 Could you give some example please?.
你能举一些例子吗?。
I trying我在尝试
while read line
do
echo "$line"
done < "${1}" < "{/dev/stdin}"
But this does not work.但这不起作用。
You can use cat -
or cat /dev/stdin
:您可以使用
cat -
或cat /dev/stdin
:
while read line; do
# your code
done < <(cat "$1" -)
or或者
while read line; do
# your code
done < <(cat "$1" /dev/stdin)
or, if you want to read from all files passed through command line as well as stdin , you could do this:或者,如果您想从通过命令行和 stdin传递的所有文件中读取,您可以这样做:
while read line; do
# your code
done < <(cat "$@" /dev/stdin)
See also:也可以看看:
This topic seems to be helpful here: 这个话题在这里似乎很有帮助:
{ cat $1; cat; } | while read line
do
echo "$line"
done
Or just要不就
cat $1
cat
if all you're doing is printing the content如果您所做的只是打印内容
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.