2016-09-23 111 views
0

运行脚本我有此脚本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 

任何人都可以帮我吗?

+1

你的脚本没有曾经尝试从标准输入读取,所以您重定向标准输入什么没有任何效果。 –

回答

4

位置参数(又名命令行参数)与标准输入不相关。下面是一个使用既是一个例子:

$ 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 
相关问题