从 Bash 中的文件和标准输入读取

Read from a file and stdin in Bash

我想知道我是否可以编写一个 shell 脚本来接受两个参数 同时 ,一个来自文件,另一个来自标准输入。能举个例子吗?

我在努力

while read line
   do
   echo "$line"
done < "" < "{/dev/stdin}"

但这不起作用。

This topic 似乎对这里有帮助:

{ cat ; cat; } | while read line
   do
   echo "$line"
done

或者只是

cat 
cat

如果您所做的只是打印内容

您可以使用 cat -cat /dev/stdin:

while read line; do
  # your code
done < <(cat "" -)

while read line; do
  # your code
done < <(cat "" /dev/stdin)

或者,如果您想读取通过命令行 以及标准输入 传递的所有文件,您可以这样做:

while read line; do
  # your code
done < <(cat "$@" /dev/stdin)

另请参阅:

  • How to read from a file or stdin in Bash?