用于检查diff的Bash脚本

时间:2021-09-09 00:11:41

I'm trying to get this script to work for checking the output of a program (test21) against known correct result text files. I've tried playing around with a couple ways of writing it, but I can't get it to work. Any ideas, thanks!

我正在尝试使用此脚本来检查程序(test21)的输出与已知的正确结果文本文件。我试过用几种方法来编写它,但我无法让它工作。任何想法,谢谢!

#!/bin/bash                                                                                                                                                                        

arr=$(find resultfile*)
x=0


for i in $(find resultfile*);do
arr[$x]=$i
x=$(($x + 1))
done

for i in ${arr[@]}; do
echo $i
done

x=0
for i in $(find testfile*);
do
    if diff  ${arr[$x]} <(./test21< testfile1.txt 2>&1); then
        echo Everything is correct
    else
        echo Everything is wrong
    fi
    x=$(($x+1))

done

It says for this,I get:

它说,为此,我得到:

Segmentation fault: 11  ./test21 < "testfile1.txt" 2>&1

My program test21 takes in an input text file and parses it, which could result in the segmentation faults. But if I use ./test21 testfile1.txt my program runs perfectly fine. I hardcoded in testfile1.txt, but it should be

我的程序test21接收一个输入文本文件并解析它,这可能导致分段错误。但是,如果我使用./test21 testfile1.txt,我的程序运行完全正常。我在testfile1.txt中硬编码,但它应该是

if diff  ${arr[$x]} <(./test21< $i 2>&1); then

However, it's reading it as $i, even if I do "$i".

然而,即使我做“$ i”,它也会以$ i的形式阅读。

2 个解决方案

#1


1  

I get:

Segmentation fault: 11  ./test21 < "testfile1.txt" 2>&1

My program test21 takes in an input text file and parses it, which could result in the segmentation faults. But if I use ./test21 testfile1.txt my program runs perfectly fine.

我的程序test21接收一个输入文本文件并解析它,这可能导致分段错误。但是,如果我使用./test21 testfile1.txt,我的程序运行完全正常。

Then just use that; instead of

然后就这样使用;代替

./test21< testfile1.txt 2>&1

write

./test21 testfile1.txt 2>&1

#2


0  

Lets say that you want to run a program myprogram which will use input.txt file as input. You want to compare output of your program with some known correct result which is written in correct.txt file. We will save output of your program in actual.txt file.

让我们说你想运行一个程序myprogram,它将使用input.txt文件作为输入。您希望将程序的输出与一些已知的正确结果进行比较,该结果是在correct.txt文件中编写的。我们将在program.txt文件中保存程序的输出。

#!/bin/bash
./myprogram < input.txt > actual.txt
cmp -s actual.txt correct.txt
if [ $? -eq 1 ]; then
    echo "WRONG"
else
   echo "CORRECT"
fi

Note that actual.txt will capture only output that came from stdout. If out want to redirect both stdout and stderr you then want to use this command:

请注意,actual.txt将仅捕获来自stdout的输出。如果想要重定向stdout和stderr,那么你想使用这个命令:

./myprogram < input.txt &> actual.txt

If you want to ignore stderr and throw it in "black hole" you will use:

如果你想忽略stderr并把它扔进“黑洞”,你将使用:

./myprogram < input.txt 2> /dev/null > actual.txt

#1


1  

I get:

Segmentation fault: 11  ./test21 < "testfile1.txt" 2>&1

My program test21 takes in an input text file and parses it, which could result in the segmentation faults. But if I use ./test21 testfile1.txt my program runs perfectly fine.

我的程序test21接收一个输入文本文件并解析它,这可能导致分段错误。但是,如果我使用./test21 testfile1.txt,我的程序运行完全正常。

Then just use that; instead of

然后就这样使用;代替

./test21< testfile1.txt 2>&1

write

./test21 testfile1.txt 2>&1

#2


0  

Lets say that you want to run a program myprogram which will use input.txt file as input. You want to compare output of your program with some known correct result which is written in correct.txt file. We will save output of your program in actual.txt file.

让我们说你想运行一个程序myprogram,它将使用input.txt文件作为输入。您希望将程序的输出与一些已知的正确结果进行比较,该结果是在correct.txt文件中编写的。我们将在program.txt文件中保存程序的输出。

#!/bin/bash
./myprogram < input.txt > actual.txt
cmp -s actual.txt correct.txt
if [ $? -eq 1 ]; then
    echo "WRONG"
else
   echo "CORRECT"
fi

Note that actual.txt will capture only output that came from stdout. If out want to redirect both stdout and stderr you then want to use this command:

请注意,actual.txt将仅捕获来自stdout的输出。如果想要重定向stdout和stderr,那么你想使用这个命令:

./myprogram < input.txt &> actual.txt

If you want to ignore stderr and throw it in "black hole" you will use:

如果你想忽略stderr并把它扔进“黑洞”,你将使用:

./myprogram < input.txt 2> /dev/null > actual.txt