Solving Alice's Shell Script Doubt for Comparing & Generating Scores

  • Thread starter Thread starter alice06
  • Start date Start date
  • Tags Tags
    Doubt Shell
Click For Summary
SUMMARY

Alice seeks assistance in creating a shell script to compare student output files against correct answers. The required script should navigate through folders named "name1" to "name30", comparing "output_a.txt" and "output_b.txt" with "correct_a.txt" and "correct_b.txt". The output should be saved in "scorei.txt" for each student, indicating which answers are correct or incorrect. Various solutions were proposed, including a Perl script and a C shell script, with emphasis on proper syntax and file existence checks.

PREREQUISITES
  • Basic understanding of shell scripting
  • Familiarity with the 'diff' command
  • Knowledge of Perl syntax and execution
  • Experience with file handling in Unix-like environments
NEXT STEPS
  • Learn advanced shell scripting techniques for file manipulation
  • Explore Perl file handling and error checking
  • Study the 'diff' command options for detailed output
  • Investigate how to handle command-line arguments in scripts
USEFUL FOR

Students, educators, and developers involved in grading or evaluating programming assignments, particularly those using shell scripts for file comparisons.

alice06
Messages
6
Reaction score
0
Hi all

30 students have sent me their answers to me in folders, "name1", "name2", ..., "name30".
Each folder contains 2 outputs, "output_a.txt" and "output_b.txt"
Now I have the correct answer "correct_a.txt" and "correct_b.txt" with me.

I know that using the command diff, I can compare 2 files ::
Code:
$ diff correct_a.txt output_a_.txt

But how do I make a shell script that goes inside each folder "namei" (say for the ith student) and compares the outputs "output_a.txt" and "output_b.txt" with the corresponding correct answers "correct_a.txt" and "correct_b.txt"
AND generate a file "scorei.txt" (say, for the ith student) that will tell me which of their answers is correct and which is wrong.

PLEASE HELP ME OUT, SINCE I NEED TO CHECK THEM URGENTLY

regards
Alice
 
Physics news on Phys.org
perl -e 'for (`ls`) { chomp; `diff correct_a.txt $_/output_a.txt > score-$_.txt`; `diff correct_b.txt $_/output_b.txt >> score-$_.txt`; }'

No, I didn't test this.
 
Not working

Thanx Coin for replying,

I don't know Perl, so JUST TO CHECK, I tried ur method.

I made a script "script.pl"
Code:
#!/usr/bin/perl
perl -e
for ($count = 1; $count <= 1; $count++) 
{ chomp; `diff correct_a.txt $_/output_a.txt > score-$_.txt`; }

Now i pasted the 2 files "correct_a.txt" and "output_a.txt" at the same directory where "script.pl" is.

Then I ran :
Code:
$ perl script.pl
But there are errors
Code:
syntax error at script1.pl line 2, near "perl -e"
syntax error at script1.pl line 4, near "++) 
"
syntax error at script1.pl line 4, near "; }"
Execution of script1.pl aborted due to compilation errors.

Please Help
Regards
Alice
 
Alice-- perl -e 'somethinggoeshere' when run at the command line just tells perl to execute 'somethinggoeshere' as if it was the contents of a file. So I believe your quoted script will work fine so long as you remove the "perl -e".

EDIT: Also a couple of other things looking at that--

The sample I gave above uses the "default variable" $_ in certain ways ("chomp" for example operates on $_) which won't work in your sample. What you probably want is something more like

#!/usr/bin/perl
for ($count = 1; $count <= 1; $count++)
{ chomp; `diff correct_a.txt $count/output_a.txt > score-$count.txt`; }

Again haven't tested it.
 
Last edited:
Code:
#!/usr/bin/perl
foreach my $dir (`ls -1`) {
    next unless($dir =~ /name(\d+)/);
    chomp $dir;
    my($i) = $1;
    `echo output_a: > score$i`;
    if(-e '$dir/output_a.txt') {
        `diff correct_a.txt $dir/output_a.txt >> score$i`;
    } else {
        `echo no output_a >> score$i`;
    }
    `echo output_b: >> score$i`;
    if(-e '$dir/output_b.txt') {
        `diff correct_b.txt $dir/output_b.txt >> score$i`;
    } else {
        `echo no output_b >> score$i`;
    }
}

Also haven't tested, but it should do the job. Also, keep in mind that diff won't return anything if the files match OR if there's a file missing. So a student with no output files would appear the same as a student with a perfect score. diff will just quietly warn you via STDERR.

DaveE
 
Last edited:
the other replies may be correct but I thought it was an interesting question. So, here's my two cents...

Code:
#!/bin/csh

foreach dir (`ls`)
	foreach file (`ls $dir`)
		set filetype = `awk file=$file '{if (file ~ /output_a/ || file ~ /output_b) print file'`
		if ("$filetype" == "output_a") then
			set compare = `diff $1 $file`
			echo $compare >> ./score{$dir}.txt
		endif
		if ("$filetype == "output_b") then
			set compare = `diff $2 $file`
			echo $compare >> ./score{$dir}.txt
		endif
	end
end