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

时间:2016-09-23 00:19:29

标签: bash shell

我有这个脚本myprogram

UISearchController

和输入文件test.txt

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

我希望使用输入重定向来运行带有test.txt输入的脚本,该输出应输出

Hi
Hello

我试图使用

This is the first one Hi.
This is the second one Hello.

但它没有用。打印出的唯一内容是

./myprogram < test.txt

任何人都可以帮我吗?

1 个答案:

答案 0 :(得分:4)

位置参数(也称为命令行参数)与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