1 Replies - 159 Views - Last Post: 07 March 2019 - 06:34 AM

#1 any43123   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 02-March 19

Small script Question LINUX

Posted 05 March 2019 - 07:26 PM

I have the second question done and the third is based off the same script. Question: i need to modify the previous script to show student schedule in the same format as the previous script showed student schedule.

the previous question and answer with result:

Create a script that prints out a schedule for a selected faculty member. The output could look like the following, but you are free to make adjust the format. Print the script and a screen print of the output as indicated below.

Answer:
###########################
#
###########################
clear
echo -n "Enter the name of a faculty member: "; read facname
echo $facname > gstemp1
tr -s [a-z] [A-Z] < gstemp1 > gstemp2
fgrep -f gstemp2 gsfaculty.txt | cut -c 1-8 > gstemp3
fgrep -f gstemp3 gsclasses.txt > gstemp4
echo "Faculty Schedule for: $facname"
echo "Course  Days    Time    Location"
awk -F# '{printf "%s\t %s\t %s\t %s\n", $1, $2, $3, $4}' gstemp4



Result:

Posted Image

These are the text files associated with the script that make it function:

gsstudents.txt
E1000001#JOHN DOE
E1000002#MARY ELIZABETH DOE
E1000003#WANDA SUE DOE


gsclasses.txt
CGE357#M-W-F#8:00 AM CST#DA212#E0000002
CGE425#T-R#12:50 PM CST#DA211#E0000001
CGE477#T-R#9:40 AM CST#DA212#E0000003
CS111#M#6:30 PM CST#DA211#E0000004
CS111#M-W#3:30 PM CST#DA211#E0000004
CS111#M-W-F#10:30 AM CST#DA211#E0000002
CS205#T-R#12:50 PM CST#DA204#E0000002
CS255#T-R#12:50 PM CST#DA212#E0000003
CS315#T-R#9:40 AM CST#DA204#E0000002
CS320#M-W-F#1:00 PM CST#DA212#E0000003
CS360#M-W-F#9:15 AM CST#DA212#E0000002
CS435#M-W-F#11:45 PM CST#DA212#E0000003
CS475#T-R#9:40 AM CST#DA212#E0000003
CS504#T#6:30 PM CST#DA204#E0000002
CS511#T#6:30 PM CST#DA204#E0000002
IS224#M-W-F#2:15 PM CST#DA212#E0000001
IS425#M#6:30 PM CST#DA213#E0000001
MDS523#ONLINE#N/A#N/A#E0000001
MDS534#ONLINE#N/A#N/A#E0000001
MTH345#M-W-F#1:00 PM CST#DA305#E0000004


gsfaculty.txt
E0000001#JASON KIM#123-45-6789#250000.00
E0000002#ARNOLD GHANE#222-22-2222#500000.00
E0000003#JAKE JONES#333-33-3333#100000.00
E0000004#BLAKE ZACK#444-44-4444#10.00


gsstudentclasses.txt
E1000001#CS111
E1000001#CS205
E1000001#CGE425
E1000001#MTH345
E1000002#CS111
E1000002#CS205
E1000002#IS425
E1000002#MTH345
E1000003#CS111
E1000003#CS205
E1000003#IS425



And this is a mapping of the files

Posted Image

This is what i need help on basically just tweaking previous script:

Create a script that prints out a schedule for a selected student. The output and approach will be the same as used for the faculty member in Project #2. Print the script and a screen print of the output as indicated below.


This was my modified attempt of the previous script. I believe there should be one line extra but not sure how to do it.

###########################
#
###########################
clear
echo -n "Enter the name of a Student: "; read stdname
echo $stdname > stdgstemp1
tr -s [a-z] [A-Z] < stdgstemp1 > stdgstemp2
fgrep -f stdgstemp2 gstudents.txt | cut -c 1-8 > stdgstemp3
fgrep -f stdgstemp3 gsclasses.txt > stdgstemp4
echo "Student Schedule for: $stdname"
echo "Course  Days    Time    Location"
awk -F# '{printf "%s\t %s\t %s\t %s\n", $1, $2, $3, $4}' stdgstemp4



Question: i need to modify the previous script to show student schedule in the same format as the previous script showed Faculty schedule****.

i had a typo in question

Is This A Good Question/Topic? 0
  • +

Replies To: Small script Question LINUX

#2 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7434
  • View blog
  • Posts: 15,411
  • Joined: 16-October 07

Re: Small script Question LINUX

Posted 07 March 2019 - 06:34 AM

So, I don't care for your first answer. To start, you produce many temp files for no reason. Let's clean up what you have first:
#!/bin/sh

# don't forget that top bit

clear
# for testing, I don't want to type over and over
# comment this out and go back to user entry when you're happy
facname="Ghane" 
# echo -n "Enter the name of a faculty member: "; read facname

# no: echo $facname > gstemp1
# this is pointless, you can grep with case ignore tr -s [a-z] [A-Z] < gstemp1 > gstemp2
# insted of fgrep -f gstemp2 gsfaculty.txt | cut -c 1-8 > gstemp3
facid=`grep -i $facname gsfaculty.txt | cut -c 1-8`

echo "Faculty Schedule for: $facname"
echo "Course  Days    Time    Location"
# instead of fgrep -f gstemp3 gsclasses.txt > gstemp4
grep $facid gsclasses.txt | awk -F# '{printf "%s\t %s\t %s\t %s\n", $1, $2, $3, $4}'



Now, we can do better. Let's start thinking like a programmer. We want reusable functions and better names:
#!/bin/sh

FILE_STUDENTS="gsstudents.txt"
FILE_CLASSES="gsclasses.txt"
FILE_FACULTY="gsfaculty.txt"
FILE_STUDENT_CLASS="gsstudentclasses.txt"

facultyIdForName() {
    # Note, I'm copying your logic for clarity, but it's questionable.
    grep -i "$1" $FILE_FACULTY | cut -c 1-8
}

facultyScheduleForId() {
    echo "Course  Days    Time    Location"
    grep $1 $FILE_CLASSES | awk -F# '{printf "%s\t %s\t %s\t %s\n", $1, $2, $3, $4}'
}

facultyScheduleForName() {
    local facname=$1
    echo "Faculty Schedule for: $facname"
    facultyScheduleForId `facultyIdForName $facname`
}

# clear
# echo -n "Enter the name of a faculty member: "; read facname
facultyScheduleForName "Ghane"



Now we have the problem broken down into reasonable bits, let's look at your next problem.
#!/bin/sh

FILE_STUDENTS="gsstudents.txt"
FILE_CLASSES="gsclasses.txt"
FILE_FACULTY="gsfaculty.txt"
FILE_STUDENT_CLASS="gsstudentclasses.txt"

studentIdForName() {
    # your code here
}

classIdsForStudentId() {
    # your code here
}

classForClassId() {
    # your code here
}

testing() {
    local stdname="John"
    echo "test: studentIdForName $stdname"
    studentIdForName $stdname
    echo "---"

    local studId=`studentIdForName $stdname`
    echo "test: classIdsForStudentId $studId"
    classIdsForStudentId $studId
    echo "---"

    echo "test: classForClassId CGE357"
    classForClassId CGE357
    echo "---"

    echo "test: classes for class ids"
    echo "this code is a big bloody hint"
    for id in `classIdsForStudentId $studId`; do
        classForClassId $id
    done
    echo "---"
}

testing



Results:
test: studentIdForName John
E1000001
---
test: classIdsForStudentId E1000001
CS111
CS205
CGE425
MTH345
---
test: classForClassId CGE357
CGE357   M-W-F   8:00 AM CST     DA212
---
test: classes for class ids
this code is a big bloody hint
CS111    M       6:30 PM CST     DA211
CS111    M-W     3:30 PM CST     DA211
CS111    M-W-F   10:30 AM CST    DA211
CS205    T-R     12:50 PM CST    DA204
CGE425   T-R     12:50 PM CST    DA211
MTH345   M-W-F   1:00 PM CST     DA305
---



I should note that if you're allowed to use awk, it can do the work of many smaller programs and you might want to take advantage of that. You can basically get rid of grep and cut, at the very least.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1