I'm trying to write a script that takes input from a text file, runs the input in a C program and determines if the answer is correct or not. The text file includes the input value and the correct value separated by a comma. The problem is that it's not working for -20. I think it's something to do with the - symbol because if I change the text file to have a space in front or remove the - symbol it works. Ill put the C programs, text file and script down below. I also included a sample output. The highlighted lines shouldn't be there. Thanks in advance!
(nb. I'm not allowed to change the text file as a valid solution)
C programs
#include <stdio.h>
int increment(int a);
void main(void){
int a;
scanf("%i", &a);
a = increment(a);
printf("%i\n", a);
return;
}
int increment(int a){
int b = a;
b = b 1;
return b;
}
Test Input File
1,2
-20,-19
37,38
1.4,2.4
0,1
0.0,1.0
'20','21'
lab04,lab05
Bash Script
#!/bin/bash
:
echo""
echo "start building main program"
echo "start compiling to assembly lines..."
cc main.c -S
cc increment.c -S
echo "translating to op codes"
cc main.s -c
cc increment.s -c
echo "statically linking all required op codes..."
cc main.o increment.c -o main
echo "build successfully done!"
echo "running main program"
echo ""
passedCount=0
failedCount=0
while read input
do
IFS=,
set $input
correctAnswer=($2)
#runs main with the input of $1 and stores the result in output
output=$(./main <<< $1)
if [ $output == $correctAnswer ]; then
echo "input: $1, main: $output, correct: $correctAnswer ==> passed"
passedCount=$((passedCount 1))
fi
if [ $output != $correctAnswer ]; then
echo "input: $1, main: $output, correct: $correctAnswer ==> failed"
failedCount=$((failedCount 1))
fi
done <"./test_inputs.txt"
echo ""
echo "Total Passed: $passedCount"
echo "Total Failed: $failedCount"
CodePudding user response:
set $input
throws an error when $input
starts with a dash, because set thinks that it's a flag. Use set -- $input
instead:
-- Assign any remaining arguments to the positional parameters. If there are no remaining arguments, the positional parameters are unset.
— help set