1

I am learning shell scripting and have a following requirement:

Req.: Need to collect the grades from a students grade file and create files having respective grades in it.

Sample file:

eng_student_grades.csv

id | div | grd
001| ER | B
002| JI | C
003| ER | B
004| LO | A
005| LO | A
006| KI | D
007| JI | A

Wrote this following piece of code in shell:

$source_table = student_rcrd
$filename = eng_student_grades.csv
$target_loc = /home/grades




if echo "$source_table" | grep 'STDNT' >> $JOBLOG; then
    for filename in `ls`;
    do
      if cat "$filename" | grep 'A'; then
          target_dir="GRD_A"
      elif cat "$filename" | grep 'B'; then
          target_dir="GRD_B"
      elif cat "$filename" | grep 'C'; then
          target_dir="GRD_C"
      elif cat "$filename" | grep 'D'; then
          target_dir="GRD_D"
      else
          continue
      fi

      newfilename=$target_dir"_"$filename".txt"
      sudo cp $filename $newfilename
      sudo zip $newfilename.zip $newfilename

But the code is not producing the expected results.

Actual O/P:

GRD_A_eng_student_grades.txt

Expected O/P:

GRD_A_eng_student_grades.txt
GRD_B_eng_student_grades.txt
GRD_C_eng_student_grades.txt
GRD_D_eng_student_grades.txt

Not getting where I'm making mistake. Any help to resolve the issue would be really helpful. Thanks in advance for your time.

rocking
  • 109
  • 2
  • 7
  • `$foo = bar` is not a valid assignment in shell. – Charles Duffy Jun 19 '19 at 23:27
  • Generally, it's a good practice to run your code through http://shellcheck.net/ before asking questions here. – Charles Duffy Jun 19 '19 at 23:27
  • ...that said, before shellcheck can give you a full analysis, you need to fix your syntax. Towards that end, note that `eif` is not valid syntax; it needs to be `elif`; and that you need a `done` and a `fi` at the end to close your loop and conditional. With those things fixed, you'll get a full list of warnings, each one with a separate wiki link. – Charles Duffy Jun 19 '19 at 23:30
  • 1
    It seems that the question has been misunderstood. The question is about the file generation based on the contents inside it. – UnicornUnion Jul 02 '19 at 06:23

0 Answers0