Shell:使用输入重定向运行带有标准输入的脚本



我有这个脚本myprogram

#!/bin/bash
echo This is the first one ${1}.
echo This is the second one ${2}.

和输入文件test.txt

Hi
Hello
我希望使用输入重定向来运行带有test.txt输入的脚本,它应该输出
This is the first one Hi.
This is the second one Hello.

我想使用

./myprogram < test.txt

但它不起作用。它输出的唯一内容是

This is the first one
This is the second one
谁能帮我一下吗?

位置参数(即命令行参数)与stdin无关。下面是两者都使用的例子:

$ cat myscript
#!/bin/bash
echo "These are the first two arguments: $1 and $2"
read -r first
echo "This is the first input line on stdin: $first"
read -r second
echo "This is the second input line on stdin: $second"
$ ./myscript foo bar < test.txt
These are the first two arguments: foo and bar
This is the first input line on stdin: Hi
This is the second input line on stdin: Hello

最新更新