Question

8. (4 pts + 2Xc) Write a bash shell script called 08-numMajors that will do the following i. Read data from a class enrollmen

1 Marketing $ 08-numMajorsk/NetworkingClass/08-ClassEnrollment-ciS350.txt #file in other air 4 Computer Information Systems 2

8. (4 pts + 2Xc) Write a bash shell script called 08-numMajors that will do the following i. Read data from a class enrollment file that will be specified on the command line ii. If the file does not exist, is a directory, or there are more or less than one parameters provided, display an appropriate error/usage message and exit gracefully Display the number of a specified major who are taking a given class iii. The following is a sample input file and a sample run. I will use a file in the same format as this one to test your script, but the one I use may have different data in it. Your script should work with whatever data is in the file I use, including other majors than the ones shown in the examples below. Sample input file (turk/NetworkingClass/08-ClassEnrollment-CIS350.txt) Bill Both, 123456789, Computer Information Systems Carla Carothers, 234567890, Computer Information Systems Stephanie Williams, 345678901, Marketing Aritrya Badopadhi, 456789012, Computer Science Santhi Roopashree, 567890123, Computer Information Systems Heather Williams, 678901234, Computer Information Systems Dave Schroth, 789012345, Computer Science Sample runs 08-nunMajors 08-ClassEnrollment-C1S350.txt #file in dir current 4 Computer Information Systems 2 Computer Science
1 Marketing $ 08-numMajorsk/NetworkingClass/08-ClassEnrollment-ciS350.txt #file in other air 4 Computer Information Systems 2 1 Marketing Computer Science 08-numMajors ClassEnrollmentFile.txt #file doesn't exist ERROR: file 'ClassEnrollmentFile.txt' does not exist. EXTRA CREDIT: For extra credit, make your output read like in the following example. Specifically: for any major with just one student, say "is"; for majors with more than one student, say "are". $ 08-numMajors -turk/NetworkingClass/08-ClassEnrollment-CIS350.txt There are 4 'Computer Information Systems' majors. There are 2 'Computer Science' majors. There is 1 'Marketing' major There are a total of 7 students in this class.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the bash script to find the count of majors

Script:

#!/usr/bin/ksh
#using awk to manipulated the input file

if [ $# != 1 ]
then
echo "Usage: 08-numMajors.sh <file-name>"
exit 1
fi


if [ -f $1 ]
then

awk '
BEGIN {
FS=","
i=0
flag=0
major[0]=""
output[0]=0
total=0
}
{
if ($3)
{

for (j=0; j<i; j++)
{
# printf("1-%s-%s\n", major[j], $3);

if(major[j] == $3)
{
flag=1
output[j] = output[j] + 1;
total++
}
}
if (flag == 0)
{
major[i]=$3
output[i] = output[i] + 1;
i++;
total++
}
else
{
flag=0
}

}
}

END {
for (j=0; j<i; j++)
{
if(output[j] > 1)
printf("There are %d `%s` majors\n", output[j], major[j]);
else
printf("There is %d `%s` major\n", output[j], major[j]);

}
printf("There are a total of %d students in this class\n", total);
}
' < $1

else

echo "Error: file $1 does not exist"
fi

Output:

USER 1> sh 08-numMajors.sh sample.txt
There are 4 ` Computer Information Systems` majors
There is 1 ` Marketing` major
There are 2 ` Computer Science` majors
There are a total of 7 students in this class


USER 1> sh 08-numMajors.sh sample.txtdf
Error: file sample.txtdf does not exist


USER 1> sh 08-numMajors.sh
Usage: 08-numMajors.sh <file-name>

USER 1> sh 08-numMajors.sh file fiel2
Usage: 08-numMajors.sh <file-name>

Add a comment
Know the answer?
Add Answer to:
8. (4 pts + 2Xc) Write a bash shell script called 08-numMajors that will do the following i. Read data from a class enrollment file that will be specified on the command line ii. If the file does no...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT