2016-09-23 75 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 
相關問題