关于路径规范和输出比较的混乱

关于路径规范和输出比较的混乱

基本上,我创建了一组测试,我想运行它们,运行测试,然后将它们与我从预先制作的输出文件中获得的测试进行比较。

到目前为止我已经得到了这个

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.

不管怎样,这在多个层面上可能都是根本错误的,但我是 shell 新手,所以这对我提高理解非常有帮助

答案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)" 

相关内容