[英]Will running a shell script from a c++ program automatically display the output of the shell script to the console?
[英]Shell Script: How to read standard output of a program from console
我正在嘗試編寫一個shell腳本,該腳本向程序提供不同的輸入並檢查輸出是否是預期的結果。 在完成這些測試后,我確定可執行程序中是否存在錯誤。
我使用./my_program arg1 arg2
(arg1和arg2是程序的命令行參數)在Shell腳本上運行程序。 在那之后,腳本外殼不斷為my_program
提供不同的輸入以my_program
進行測試,並在控制終端(或控制台)中反復編寫標准輸出,如下所示:
Connection established.
Intermediate result is expected_intermediate_result1
Final result is expected_result1
Connection established.
Intermediate result is expected_intermediate_result2
Final result is expected_result2
它繼續。 對於每個輸入,其輸出都是已知的。 因此,它們之前是匹配的。
連接失敗時:寫入Error in connection!
否則結果可能是錯誤的:
Connection established.
Intermediate result is result1
Final result is wrong_result1
該腳本除了提供輸入外,還有另一個目的:檢查結果。
因此,我想從控制台讀取輸出並將它們與預期結果進行比較,以確定是否存在不一致的情況。
我希望您的協助來修改此代碼:
while read console line-by-line
if the line is other than expected result
store this case to text file
done
注意事項:我不想使用expect
。 我只想讀取在控制台中編寫的程序的輸出。 我不使用日志文件,因此不會使用文件( grep
)中的搜索。
感謝您的堅持!
這是您要做什么?
./my_program arg1 arg2 |
grep -Fxq "Final result is expected_result1" || { printf 'Failed: "arg1 arg2" -> "expected_result1"\n'; exit 1; }
如果沒有,請編輯您的問題以闡明您的要求並提供一個更具體的示例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.