Bash脚本自动测试程序输出 – C.

前端之家收集整理的这篇文章主要介绍了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 missing\n" "$file_in"
        continue;
    fi
    if [ ! -f "$file_out_val" ]; then
        printf "Validation file %s is missing\n" "$file_out_val"
        continue;
    fi

    printf "Testing against %s\n" "$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 : %d\n" "$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
>输出要验证的文件:file1.out
>输出文件:file1.out.tst
>在文件中:file2.in
> ……

>通过<执行应用程序并将文件重定向到stdin以供应用程序使用,并通过>将stdout从应用程序重定向到out文件测试.
>使用像diff这样的工具来测试它们是否相同.
>检查工具和打印消息中的退出/返回代码(FAIL / OK)
>提示继续.

任何和所有当然可以修改,删除等.

一些链接

> TLDP; Advanced Bash-Scripting Guide(this可以更具可读性)

> Arrays
> File test operators
> Loops and branches
> @L_301_5@
> ……

> bash-array-tutorial
> TLDP; Bash-Beginners-Guide

原文链接:https://www.f2er.com/bash/383526.html

猜你在找的Bash相关文章