13 Teach : Team Activity
Overview
This activity will help you practice writing shell scripts in Linux.
Your assignment is to write a shell script that accepts a directory as a command line parameter, and then iterates through the .py files in that directory.
Instructions
Core Requirements
Create a shell script that accepts a file as a command line parameter and then displays it to the screen as follows: (The dollar sign below just means you are typing it at the command line)
Make your script executable and ensure that it runs.
Have your script count the number of lines in that file and display it to the screen. For example:
Change your script to accept a directory (rather than a file) as the command line parameter. Then have it iterate through all
.pyfiles in that directory and for each one, display the name of the file and the number of lines in that file.
$ ./lineCounter.sh myFile.py
Running on file: myFile.py
$ ./lineCounter.sh myFile.py
Running on file: myFile.py
127 myFile.py
$ ./lineCounter.sh /home/cs241
Running on directory: /home/cs241
Files in the directory:
40 /home/cs241/check03b.py
63 /home/cs241/check04b.py
62 /home/cs241/check05a.py
37 /home/cs241/check07a.py
55 /home/cs241/check07b.py
Stretch Challenges
After completing the above steps. Make sure that everyone on the entire team is to this point and understands the material. Then, if you have time, move onto the following stretch challenges.
Change your script so that the number of lines per file is saved to an output file, rather than to the screen. Specify the output file with another parameter (e.g.,
./lineCounter.sh /home/cs241 pyLines.txt)Change your script so that it looks through sub-directories (just one level deep) for
.pyfiles as well.Change your script so that in addition to accepting a directory name, it also accepts a list of potential extensions (e.g.,
./lineCounter.sh /home/cs241 pyLines.txt py cpp txt). Then, have it count the lines in all of those types of files.
Instructor's Solution
As a part of this team activity, you are expected to look over a solution from the instructor, to compare your approach to that one. One of the questions on the I-Learn submission will ask you to provide insights from this comparison.
Please DO NOT open the solution until you have worked through this activity as a team for the one hour period. At the end of the hour, if you are still struggling with some of the core requirements, you are welcome to view the instructor's solution and use it to help you complete your own code. Even if you use the instructor's code to help you, you are welcome to report that you finished the core requirements, if you code them up yourself.
After working with your team for the one hour activity, click here for the instructor's solution.
Submission
You do not need to submit your program (just make sure that everyone has a copy of it for their reference). Instead, answer the accompanying questions in the I-Learn quiz.