Bash脚本自动测试程序输出 – C.
我是编写脚本的新手,我无法弄清楚如何开始使用bash脚本,该脚本将根据预期输出自动测试程序的输出.
我想编写一个bash脚本,它将在一组测试输入上运行指定的可执行文件,例如in1 in2等,对应相应的预期输出,out1,out2等,并检查它们是否匹配.要测试的文件从stdin读取其输入并将其输出写入stdout.因此,在输入文件上执行测试程序将涉及I / O重定向. 将使用单个参数调用该脚本,该参数将是要测试的可执行文件的名称. 我正在努力解决这个问题,所以任何帮助(链接到任何进一步解释我如何做到这一点的资源)将不胜感激.我显然已经尝试过自己寻找,但在这方面并不是很成功. 谢谢!
如果我得到你想要的东西;这可能会让你开始:
混合使用像diff这样的bash外部工具. #!/bin/bash # If number of arguments less then 1; print usage and exit if [ $# -lt 1 ]; then printf "Usage: %s <application>n" "$0" >&2 exit 1 fi bin="$1" # The application (from command arg) diff="diff -iad" # Diff command,or what ever # An array,do not have to declare it,but is supposedly faster declare -a file_base=("file1" "file2" "file3") # Loop the array for file in "${file_base[@]}"; do # Padd file_base with suffixes file_in="$file.in" # The in file file_out_val="$file.out" # The out file to check against file_out_tst="$file.out.tst" # The outfile from test application # Validate infile exists (do the same for out validate file) if [ ! -f "$file_in" ]; then printf "In file %s is missingn" "$file_in" continue; fi if [ ! -f "$file_out_val" ]; then printf "Validation file %s is missingn" "$file_out_val" continue; fi printf "Testing against %sn" "$file_in" # Run application,redirect in file to app,and output to out file "./$bin" < "$file_in" > "$file_out_tst" # Execute diff $diff "$file_out_tst" "$file_out_val" # Check exit code from previous command (ie diff) # We need to add this to a variable else we can't print it # as it will be changed by the if [ # Iff not 0 then the files differ (at least with diff) e_code=$? if [ $e_code != 0 ]; then printf "TEST FAIL : %dn" "$e_code" else printf "TEST OK!n" fi # Pause by prompt read -p "Enter a to abort,anything else to continue: " input_data # Iff input is "a" then abort [ "$input_data" == "a" ] && break done # Clean exit with status 0 exit 0 编辑. 添加退出代码检查;还有一个很短的步行槽: 这将简短地做: >检查是否给出了参数(bin / application) > I.e.:你得到数组(“file1”“file2”) >在文件中:file1.in >通过<执行应用程序并将文件重定向到stdin以供应用程序使用,并通过>将stdout从应用程序重定向到out文件测试. 任何和所有当然可以修改,删除等. 一些链接: > TLDP; Advanced Bash-Scripting Guide(this可以更具可读性) > Arrays > bash-array-tutorial (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |