PDF Version Available
This document is also available in PDF format: shellloop.pdf
The PDF version includes bookmarks for easy navigation and is optimized for printing.
Accessibility Notice
This document is also available in HTML format at:
Link: https://aholdengouveia.name/IntroLinux/labs/shellloop.html
The HTML version provides enhanced accessibility features including keyboard navigation, screen reader support, responsive design, dark mode support, and high contrast options.
Learning Outcomes
By the end of this lab, you will be able to:
- Write bash scripts that use count controlled loops
- Use user input or positional parameters to control how many times a loop runs
- Test a script with different inputs, and explain what each line does by writing comments
Lab Instructions
This lab has 2 scripts. Each one must be a separate script.
Script 1: Repeat a Phrase
Write a script that will echo a phrase for a given number of times. The user should generate the phrase and the number of times it is echoed. You may use read or positional parameters, but you need to include a comment about which one you picked and why. Include a comment with your name. Include test runs with at least two different phrases and counts. One of your test phrases must be the full name of another teacher at our school (not me).
Script 2: Countdown
Write a script that will do a countdown. The user should be able to specify what number is started with, the countdown should go to 1. Include test runs counting down from at least two different starting numbers. One of them must start from the number of days or weeks left in our semester, until the last day of classes.
Tips
It's better to start simple and work your way up. I have a video on my Linux FAQ playlist on YouTube (linked under Resources) if you need help. Don't try to write the whole loop at once, start with the basics, use my example if you want, and make small changes.
Deliverables
One file that includes all the requested information with your name, the date, and the lab title.
How to turn in your scripts
Follow the format in the sample submission Link: https://www.aholdengouveia.name/IntroLinux/labs/CIS%20117%20shell%20sample.docx
- Submit a copy of your code as actual text. NO screenshots of code are allowed.
- Submit a screenshot or the results of your test run, so that I can see a successful run of your code. Every script should be tested. A script that doesn't run is worth 0 points. Your screenshots must show your prompt with your username, or a note with your name and the term.
- Comments should be done on EVERY line of code, (this is very bad practice in the real world, never do this for a job) so that I can see that you understand the code that you are submitting. Every line should have an explanation of what that line does. Comments are worth 5 points per script.
Resources
References, a video, a PowerPoint and some notes are available at my website Link: https://www.aholdengouveia.name/IntroLinux/shellloop.html
My FAQ Linux playlist on YouTube, with demos of many of the commands and scripts in this class Link: https://www.youtube.com/playlist?list=PLQOmuLC45eoQlIMH7oCNL6yFP7kWQubBo