パス仕様と出力比較の混乱

パス仕様と出力比較の混乱

基本的に私は一連のテストを生成し、それを実行し、テストを実行し、事前に作成された出力ファイルから得られたテストと比較したいと思います。

これまで私はこれを得ました

for file in ./tests/*.test; do
# Now I'm unsure how can I get the output of a file stored in a variable.
# I did this, but I'm unsure whether it's correct
./myexec "$file"
returned=$?
# So if my understanding is correct, this should run my desired test and store STDOUT
# Next I want to compare it to what I have inside my output file
compared="$(diff returned ./tests/"$file".output)"
if [ -z $compared ]; then
   echo "Test was successful"
   passed=$((passed + 1))
else
   echo "Test was unsuccessful, got $ret but expected ./tests/"$file".output")
   failed=$((failed + 1))
# I presume that one above is an incorrect way to print out the expected result
# But I couldn't really think of anything better.

とにかく、これはいくつかのレベルで根本的に間違っているかもしれませんが、私はシェルを初めて使用するので、理解を深めるのに非常に役立ちます。

ベストアンサー1

returned=$?STDOUTはに保存されませんreturned。これは、最後に実行されたコマンドの終了コードを保存します./myexec "$file"

./tests/"$file".output予想される結果が維持されると仮定すると、たとえば次のようになります。

# first assign the value properly using the command substitution
return=$(./myexec $file)

# Then compare using process substitution, as diff demands a file to compare.
## And the process  substitution passes diff the file descriptor of its STDOUT.
compared="$(diff <(echo "$returned") ./tests/"$file".output)" 

おすすめ記事