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

Discussion Overview

The discussion revolves around creating a shell script to compare student output files with correct answers and generate score files. The context includes scripting in Perl and C shell, with a focus on file handling and comparison using the diff command.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Exploratory

Main Points Raised

  • Alice describes the need for a script to compare student outputs with correct answers and generate score files for each student.
  • One participant suggests a Perl one-liner to perform the comparisons but notes that it hasn't been tested.
  • Alice attempts to implement the suggested Perl script but encounters syntax errors, indicating a lack of familiarity with Perl.
  • Another participant clarifies that the Perl command should not include "perl -e" when written in a script and provides a modified version of the script for Alice.
  • A different Perl script is proposed, which includes checks for the existence of output files and handles missing files by echoing messages into the score files.
  • A participant introduces a C shell script as an alternative approach, outlining a method to iterate through directories and compare files, but does not confirm its functionality.

Areas of Agreement / Disagreement

Participants present various approaches to the problem, but there is no consensus on a single correct solution. Some scripts are proposed without testing, and there are differing opinions on the best method to achieve the desired outcome.

Contextual Notes

Some scripts may contain syntax errors or logical issues that have not been resolved. The effectiveness of the proposed solutions remains uncertain as participants have not tested all scripts thoroughly.

Who May Find This Useful

Individuals interested in shell scripting, particularly in Perl and C shell, as well as those looking for methods to automate file comparisons and score generation in educational contexts.

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