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

In summary: foreach#!/bin/bashforeach dir (`ls`) foreach file (`ls $dir`) set filetype = `cat file` set compare = `diff $file $file` echo $compare >> ./score{$dir}.txt endforeach
  • #1
alice06
6
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
  • #2
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.
 
  • #3
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
 
  • #4
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:
  • #5
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:
  • #6
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
 

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

1. How do I compare scores in Alice's shell script?

In order to compare scores in Alice's shell script, you will need to use conditional statements such as if or case statements. These will allow you to check if one score is greater than, less than, or equal to another score.

2. Can I use variables in Alice's shell script to store scores?

Yes, you can use variables to store scores in Alice's shell script. This will allow you to easily access and compare scores throughout your script. Make sure to assign the correct data type to your variables, such as int for integer scores or float for decimal scores.

3. How can I generate scores in Alice's shell script?

You can use a variety of methods to generate scores in Alice's shell script. One option is to use the shuf command to randomly select a number within a specified range. Another option is to use mathematical operations, such as expr or bc, to generate scores based on certain criteria.

4. What are some best practices for solving Alice's shell script doubt for comparing and generating scores?

Some best practices for solving Alice's shell script doubt for comparing and generating scores include: using meaningful variable names, properly indenting your code for readability, and testing your script with different inputs to ensure accuracy. It is also important to break down the problem into smaller, manageable tasks and tackle them one at a time.

5. Can I use loops in Alice's shell script to compare and generate scores?

Yes, you can use loops in Alice's shell script to compare and generate scores. Loops allow you to repeat a set of instructions multiple times, which can be useful for generating multiple scores or comparing scores in a list. Make sure to properly condition your loops to avoid endless iterations.

Back
Top