我正在編寫一個shell腳本,它遍歷我的./tests目錄並使用unix diff命令將我的C程序中的.in和.out文件相互比較。這裏是我的shell腳本:如果diff命令導致bash無差異,如何輸出'passed'?
#! /usr/bin/env bash
count=0
# Loop through test files
for t in tests/*.in; do
echo '================================================================'
echo ' Test' $count
echo '================================================================'
echo 'Testing' $t '...'
# Output results to (test).res
(./snapshot < $t) > "${t%.*}.res"
# Test with diff against the (test).out files
diff "${t%.*}.res" "${t%.*}.out"
echo '================================================================'
echo ' Memcheck
echo '================================================================'
# Output results to (test).res
(valgrind ./snapshot < $t) > "${t%.*}.res"
count=$((count+1))
done
我的問題是我怎麼能if語句添加到要輸出「通過」腳本如果沒有差異diff命令的結果嗎?例如
僞代碼:
if ((diff res_file out_file) == '') {
echo 'Passed'
} else {
printf "Failed\n\n"
diff res_file out_file
}
我已經閱讀過某處,for循環不應該用於遍歷目錄中的文件。但不記得在哪裏。 – sjsam
你知道爲什麼或者什麼是合適的工具來使用嗎? – joshuatvernon
我讀的文章建議使用while循環。但是你可能會忽略這些評論,除非有人確認或者我能夠找到源碼 – sjsam