Compare output of program to correct program using bash script, without using text files
I've been t开发者_开发技巧rying to compare the output of a program to known correct output by using a bash script without piping the output of the program to a file and then using diff on the output file and a correct output file.
I've tried setting the variables to the output and correct output and I believe it's been successful but I can't get the string comparison to work correctly. I may be wrong about the variable setting so it could be that.
What I've been writing:
TEST=`./convert testdata.txt < somesampledata.txt`
CORRECT="some correct output"
if [ "$TEST"!="$CORRECT" ];
then
echo "failed"
fi
if [ "$TEST!"!="$CORRECT" ];
Looks like you have an extra !
in $TEST!
.
This works for me...
$ echo "foobar" > /tmp/test; TEST=`tail -n1 < /tmp/test`; CORRECT="foobar"; if [ "$TEST" != "$CORRECT" ]; then echo "failed"; fi
$
This fails for me...
$ echo "barfoo" > /tmp/test; TEST=`tail -n1 < /tmp/test`; CORRECT="foobar"; if [ "$TEST" != "$CORRECT" ]; then echo "failed"; fi
failed
Problem was that I was using an incorrect string for my correct output, so it always failed.
correction:
TEST=`./convert testdata.txt < BothKnownZero.txt`
CORRECT=$'Enter original quantity, original units, new units\n0.0000 miles equals 0.0000 feet'
echo $TEST
echo $CORRECT
if [ "$TEST" != "$CORRECT" ]
then
echo "failed "
fi
If you have zsh
available you can use the =(cmd)
construct, it saves the output of cmd
to a temporary file which is deleted when the command returns:
if ! diff -q known_good =(cmd); then
# do something if they differ
fi
精彩评论