使用diff时,Bash Script退出时没有错误(Bash Script exiting without error while using diff)

编程入门 行业动态 更新时间:2024-10-22 23:11:19
使用diff时,Bash Script退出时没有错误(Bash Script exiting without error while using diff)

我很擅长使用bash,并试图创建一个自动编程脚本来运行一些测试用例。 目前我的bash脚本似乎表现得很奇怪; 当我有-e标志设置时,bash将在diff具有正大小时退出,并且当未设置-e标志时,脚本会忽略diff文件中的任何差异并且表示所有测试都已通过。

脚本在“write_diff_out = ....”命令后立即退出,不打印下一行。 我只包括脚本的差异部分,因为其他一切运行正常(文件都存在)。

# Validate outputs and print results echo "> Comparing current build's final memory output with golden memory output..."; for file in `ls test_progs`; do file=$(echo $file | cut -d '.' -f1); echo "$file"; write_diff_out=$(diff ./log/$file.writeback.out ./log/$file.writeback.gold.out > ./diff/$file.writeback.diff); echo "Finished write_diff"; program_diff_out=$(diff -u <(grep -E '@@@' ./log/$file.program.out) <(grep -E '@@@' ./log/$file.program.gold.out) > ./diff/$file.program.diff); echo "Finished program diff"; if [ -z "$write_diff_out" ] && [ -z "$program_diff_out" ]; then printf "%20s:\e[0;32mPASSED\e[0m\n" "$file"; else printf "%20s:\e[0;31mFAILED\e[0m\n" "$file"; fi done echo "> Done comparing test outputs.";

随意建议一种更好的格式化diff命令的方法,我知道有不同的编写方法。

I'm fairly new to using bash and was trying to create an autograder script for running some test cases. Currently my bash script seems to be acting strangely; when I have the -e flag set bash will just exit when a diff has a positive size, and when the -e flag is not set the script ignores any differences in the diff files and says that all tests passed.

The script exits immediately after the "write_diff_out=...." command, the next line is not printed. I've only included the diffing portion of the script as everything else runs fine (the files all exist as well).

# Validate outputs and print results echo "> Comparing current build's final memory output with golden memory output..."; for file in `ls test_progs`; do file=$(echo $file | cut -d '.' -f1); echo "$file"; write_diff_out=$(diff ./log/$file.writeback.out ./log/$file.writeback.gold.out > ./diff/$file.writeback.diff); echo "Finished write_diff"; program_diff_out=$(diff -u <(grep -E '@@@' ./log/$file.program.out) <(grep -E '@@@' ./log/$file.program.gold.out) > ./diff/$file.program.diff); echo "Finished program diff"; if [ -z "$write_diff_out" ] && [ -z "$program_diff_out" ]; then printf "%20s:\e[0;32mPASSED\e[0m\n" "$file"; else printf "%20s:\e[0;31mFAILED\e[0m\n" "$file"; fi done echo "> Done comparing test outputs.";

Feel free to suggest a better way of formatting the diff commands as well, I know there are different methods of writing them.

最满意答案

我不知道你的问题是什么,但我已经重写了你的脚本以符合一些最佳实践。 也许它会更好。

#!/bin/bash # Debugging mode: prints every command as executed, remove when uneeded set -x # Validate outputs and print results echo "> Comparing current build's final memory output with golden memory output..." cd test_progs for file in *; do file="$(echo "$file" | sed 's/\.[^.]*$//')" echo "$file" # will PASS when both diffs return non-zero if ! diff "log/$file.writeback.out" \ "log/$file.writeback.gold.out" > \ "diff/$file.writeback.diff" && \ ! diff -u <(grep -E @@@ "log/$file.program.out") \ <(grep -E @@@ "log/$file.program.gold.out") > \ "diff/$file.program.diff"; then printf '%20s:\e[0;32mPASSED\e[0m\n' "$file" else printf '%20s:\e[0;31mFAILED\e[0m\n' "$file" fi done echo "> Done comparing test outputs."

它避免解析ls, 使用引号到期 ,使用[[而不是[(你不需要在[[)中引用变量,并且它测试写入的文件是否为空而不是在变量中存储某些内容)。

如果你真的想将diff的输出存储在变量中,你可以这样做:

write_diff_out="$(diff "log/$file.writeback.out" "log/$file.writeback.gold.out" | tee "diff/$file.writeback.diff")"

然后$write_diff_out将包含diff/$file.writeback.diff文件具有的相同数据。

编辑 :编辑我的答案,在评论中实现一些事情。

I don't exactly know what's your problem, but I have rewritten your script to conform to some best practices. Perhaps it will work better.

#!/bin/bash # Debugging mode: prints every command as executed, remove when uneeded set -x # Validate outputs and print results echo "> Comparing current build's final memory output with golden memory output..." cd test_progs for file in *; do file="$(echo "$file" | sed 's/\.[^.]*$//')" echo "$file" # will PASS when both diffs return non-zero if ! diff "log/$file.writeback.out" \ "log/$file.writeback.gold.out" > \ "diff/$file.writeback.diff" && \ ! diff -u <(grep -E @@@ "log/$file.program.out") \ <(grep -E @@@ "log/$file.program.gold.out") > \ "diff/$file.program.diff"; then printf '%20s:\e[0;32mPASSED\e[0m\n' "$file" else printf '%20s:\e[0;31mFAILED\e[0m\n' "$file" fi done echo "> Done comparing test outputs."

It avoids parsing ls, use quotes where it is due, used [[ instead of [ (you don't need to quote variables inside of [[), and it tests if the written file is empty instead of storing something at a variable.

If you really wanted to store diff's output in a variable, you would do this:

write_diff_out="$(diff "log/$file.writeback.out" "log/$file.writeback.gold.out" | tee "diff/$file.writeback.diff")"

Then $write_diff_out would contain the same data the diff/$file.writeback.diff file has.

EDIT: edit my answer a bit, to implement some of the things in the comments.

更多推荐

本文发布于:2023-08-06 10:19:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1446669.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   Bash   diff   Script   error

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!