2011-08-25 27 views
1

我試圖將一個文件和一行代碼傳入程序,例如。將行和文件內容作爲stdin傳遞給程序

我行:

i := 1; 

我的文件(文件):

blah1 
blah2 
blah3 

輸入到程序:

i := 1; 
blah1 
blah2 
blah3 

我想這將是一個行如:

example < `echo "i := 1;\n" cat file` 

或類似的東西

回答

1

你需要的是這裏的字符串:

example <<< `echo "i:=1" && cat file` 

從bash的手冊:

3.6.7 Here Strings 

A variant of here documents, the format is: 

    <<< word 

The word is expanded and supplied to the command on its standard input. 
2
{ echo 'i := 1;' ; cat myfile.txt ; } | example 
1
(
echo "i := 1" 
cat file 
) | program 
+0

不,重定向到一個名爲'文件程序「,並且你缺少一個命令分隔符。 – tripleee

+0

對不起,評論似乎已經在黃輸入框?? – tripleee

+0

它可以工作,因爲它分成多行。它可以在一行中完成,但是如果可以的話我更喜歡這樣的東西 - 特別是在腳本文件中。 Bash和Ksh都允許通過終端中的多條線路進行命令輸入。 –

相關問題