Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a script that: 1) Counts file types in a directory and 2) Prints the leading athletes from a data file of athletes, sports and

Write a script that:

1) Counts file types in a directory and

2) Prints the leading athletes from a data file of athletes, sports and scores.

Script description

Script name: hw6.sh

Usage: hw6.sh directory-to-count athlete-data-file

Arguments:

directory-to-count: path to directory, no trailing /

athlete-data-file: path to file containing lines of data in this format:

firstname-lastname sport score

Summary:

File types:

Counts the different types of files in directory-to-count and presents the totals for each file type. The types are: directory, regular file with content, empty file, softlink.

Leader board:

Reads athlete names, sports, and scores from athlete-data-file.

There are three sports: luge, skiing, and cycling

Determines the athlete with the highest score in each sport

Presents the leaders in each sport

Example Output

===== HOMEWORK 6 =====

------- FILE TYPES

Files in directory: hw6_test

Directories: 2

Softlinks: 3

Files with content: 10

Empty files: 5

------- LEADER BOARD

Luge leader: Marita Score: 42

Skiing leader: Jones Score: 93

Cycling leader: Tom Score: 301

Example athlete data file

Lelia-Jones skiing 93

Marita-Lopez luge 42

Tom-Yeo cycling 301

Jean-Thierry cycling 101

Michel-Kipler luge 32

Sam-Lowry skiing 21

Test input

You can test your code with this directory and athlete data

Directory: /home/hw6_test

Athlete data: /home/hw6_test/athlete_data.txt

Requirements

Coding standard: you must follow the coding standard (see below)

Input: do not assume that the test input will be used for grading a different directory and data file will be used.

In the file types section

You must calculate the total number of files of each type:

soft links

regular files with content

regular files that are empty

directories

Note: soft links are also regular files so they will be counted twice

You must strip the path from the directory argument when you display it. If the argument was /home/hw6_test, you would display:

Directory: hw6_test

You must display the total number of files of each type

In the leader board section

You must use this pattern to iterate through the athlete data file:

while read athlete sport score do

# while body

done < $athlete_data_file

You must use a case statement in the body of the while loop, the case statement should switch on the possible values of $sport.

When displaying athlete names, you must:

Display only the first name of the leaders in luge and cycling

Display only the last name of the leader in skiing

Note: the athlete data file will always contain the names as a single string, with a - between the first and last names, e.g., lelia-jones

When printing output

Your output must follow this pattern:

===== HOMEWORK 6 =====

------- FILE TYPES

Files in directory: ??

Directories: ??

Softlinks: ??

Files with content: ??

Empty files: ??

------- LEADER BOARD

Luge leader: ?? Score: ??

Skiing leader: ?? Score: ??

Cycling leader: ?? Score: ???

Hints and how-tos

How to read a line from a file word by word

You can use read line to read a whole line from a file

You can use read name sport score.. to read a line from a file word by word.

read assumes that the words are separated by white space

read automatically creates variables for each word, using the names that you give them.

You access the variables with $variable-name

while read name sport score: do

# Access the words with $name, $sport, $score

done < athlete_data_file

How to strip the path from the directory argument

/home/hw5/some_directory

|------pathame---- ---|-----basename------|

Problem

You have a variable containing a directory name like this: /home/hw6_test

And you want to display just the basename part: hw6_test

Solution

Use a variable expansion expression (a.k.a pattern matching operator, a.k.a parameter substitution) that you learned about in lecture 5.

Coding standard

File Preamble Comments

Put preamble comments at the top of the file, right after the shebang line

Follow this template:

# Author:

#

# Summary:

#

# Arguments:

# 1:

# 2:

# etc

#

# Usage: hw6.sh directory-to-count athlete-data-file

# Example: ./hw6.sh /home/hw6_test /home/hw6_test

#

Meaningful argument names

Immediately after the preamble comments, copy the command line arguments ($1, $2, etc.) into variables with meaningful names.

Use the named arguments in the rest of the script rather than $1, $2 etc.

Example

meaningful_name_of_first_argument=$1

meaningful_name_of_second_argument=$2

Section comments

Start each logical section with a short comment giving the general intent of the section, such as:

# Rename and print the arguments

Variable names

For variables local to the script use underscore_separated_lower_case

Use meaningful variable names.

Do not use single-letter names except as loop indices.

Do not use CamelCase.

Flow control

Do not use break, continue, or exit to break out of a loop.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Management Systems Designing And Building Business Applications

Authors: Gerald V. Post

1st Edition

0072898933, 978-0072898934

More Books

Students also viewed these Databases questions

Question

=+professionalism and competency in handling global HR issues?

Answered: 1 week ago