Google Answers Logo
View Question
 
Q: Linux grade emailing script ( No Answer,   2 Comments )
Question  
Subject: Linux grade emailing script
Category: Computers > Programming
Asked by: script101-ga
List Price: $20.50
Posted: 03 Sep 2005 16:37 PDT
Expires: 03 Sep 2005 21:37 PDT
Question ID: 564017
I need a script (bash, perl or whatever) that takes a csv file called grades.csv 
and emails students their grade when I run the script.  csv file is in
the following format:
"ID","Name","Email", "Quiz 1", "Quiz 2", "Exam 1", "Total", etc
1234, "John Doe","jdoe@umn.edu",12,15,86,74
4567, "John Doe2","jdoe2@umn.edu",4,12,74,89

The student should recieve and email in the following format:
Subject:  Your Calc grade:
Dear John Doe, your grade in class is:
Quiz 1|Quiz 2|Exam 1|Total|
12    |15    |86    |74   |
---------------------------
If you have any questions regarding your grade please contact me ASAP!

Clarification of Question by script101-ga on 03 Sep 2005 19:39 PDT
Script by bozo99-ga works great except the email is not in the correct format:
Email format is:
Dear John Doe 2, your grade in class is:
Quiz 1|Quiz 2|30    |40    |
--------------
If you have any questions regarding your grade
please contact me ASAP!

I want email format to be:
Dear John Doe 2, your grade in class is:
Quiz 1|Quiz 2|
30    |40    |
--------------
If you have any questions regarding your grade
please contact me ASAP!

If you fix this little problem, I'll be pay you the money.  Thank you very much.

Clarification of Question by script101-ga on 03 Sep 2005 19:55 PDT
Dear bozo99-ga,
Could you also write a scipt for the same csv file that does the following:
If I type
./scriptname -e email@blah.edu
the scipt prints that students grades (in the same format as in email)
to the screen.
If I type
./scriptname -id 1234
the scipt prints that students grades (in the same format as in email)
to the screen.
If you are interested in writing such a script, let me know.  I am
thinking about posting this question for $5.  I think $5 is O.K. since
you already have most of the script ready.

Clarification of Question by script101-ga on 03 Sep 2005 21:20 PDT
#added printf(EMAIL "\n",$t); right before
for ($i=0; $i < @scores; $ i++) {

to fix the format problem.
Thus, I accept bozo99-ga answer.

Clarification of Question by script101-ga on 03 Sep 2005 21:35 PDT
Dear bozo99-ga,
According to Google (https://answers.google.com/answers/faq.html#cost) 

Will I be charged for more than one answer? 
No. You can only be charged for an "answer", and only one answer can
be provided to a question. You will be charged the amount you
specified when posting your question when a Researcher posts an answer
to your question on the Google Answers site. All comments posted to
your question are free.

What is the difference between a Google Answers Researcher and a registered user? 
Google Answers Researchers are screened and approved independent
contractors who are paid for posting "answers" to the site. Registered
users can ask questions and post comments to the site. However, they
cannot post "answers." Users are neither paid for comments that they
post, nor are they charged for comments that are posted to the
questions that they ask.

Thus, I don't think the system will pay you unless you are a Google researcher.
I have been trying to add your comment as an answer but the system
will not let me do it.  Let me know if you know of a way to add your
comment as an answer so I can get paid for your work.  Thank you very
much for the script.  It works great!!!
Answer  
There is no answer at this time.

Comments  
Subject: Re: Linux grade emailing script
From: bozo99-ga on 03 Sep 2005 18:47 PDT
 
I hope this isn't messed up by word-wrap or web presentation.

#!/usr/bin/perl -w

require 5.0;

@titles=();

#  might want an absolute pathname for the csv file
open(GRADES, "<grades.csv") or die("cannot open grades.csv file");

while (<GRADES>) {
    chomp();
    @f=split(/,/);
    foreach $f (@f) {
       die("NEED QUOTING - COMMAS IN THE FIELDS MAKE THIS MORE
COMPLICATED") if ($f =~ /,/);
       if ($f =~ /^\s*"(.*)"\s*$/) {
           # removed quotes and whitespace round the string
           $f=$1;
       }
    }
    if ( (0==@titles) && ("ID" eq $f[0]) ) {
        @titles=@f;
        shift @titles;
        shift @titles;
        shift @titles;
        for ($i=0; $i < @titles; $ i++) {
            # length of each title is recorded for use in counting whitespace later
            $tl{$i}=length($titles[$i]);
        }
        next;
    }
    @scores=@f;
    shift @scores;
    shift @scores;
    shift @scores;

    if (@titles != @scores) {
        printf("Warning: Number of titles does not equal number of
scores for %s (%s)\n", $f[0], $f[1]);
    }

    # Check pathname of sendmail is correct - "/usr/sbin/sendmail" on some systems.
    open(EMAIL, "|/usr/lib/sendmail -oi -t") or die("opening EMAIL
($f[0]) $!") or die("failed email");
    printf(EMAIL "To: %s\n", $f[2]);
    printf(EMAIL "Subject:  Your Calc grade:\n");
    printf(EMAIL "\n\nDear %s, your grade in class is:\n", $f[1]);

    # show titles
    foreach $t (@titles) {
        printf(EMAIL "%s|", $t);
    }    # show scores
    # I'm assuming these are always numeric as in your example and not
text like "absent".
    for ($i=0; $i < @scores; $ i++) {
        $s=$scores[$i];
        $pad=$tl{$i}  -length($s);
        printf(EMAIL "%d", $s);
        printf(EMAIL "%s|",  " "x$pad);
    }
    printf(EMAIL "\n");

    # underline of correct length
    for ($i=0; $i < @titles; $ i++) {
        $pad=$tl{$i}+1;
        printf(EMAIL "%s", "-"x$pad);
    }
    printf(EMAIL "\n");

    printf(EMAIL "If you have any questions regarding your grade
please contact me ASAP!\n");
    close(EMAIL) or die("failed email");
}
close(GRADES);

exit(0);
Subject: Re: Linux grade emailing script
From: bozo99-ga on 03 Sep 2005 18:52 PDT
 
The word-wrap is pretty obvious so I don't expect it to cause trouble.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy