PDF Version Available
This document is also available in PDF format: shellcond.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/shellcond.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 if, if/else, and if/elif/else statements
- Nest if statements so a script responds differently to different user input
- Build a script in steps, starting from a simple working version
- Test every path through a script, including correct and incorrect input, and explain what each line does by writing comments
Lab Instructions
This lab has 2 scripts. Each one must be a separate script. For each script, include two versions, clearly labelled: your first version with a single working if statement, and your finished version with both if statements nested together. Submissions that don't have both versions will be given a 0.
Script 1: Knock Knock Joke
Write a script to tell a knock knock joke. Make sure the user is giving the correct input in the correct spot. This will require a nested if statement that will only finish the joke if the correct response is given. Test that each option works! Include test runs showing the correct and incorrect answers.
Script 2: Choose Your Own Adventure
Write a script to do a choose your own adventure story about our school mascot. If you don't know what an adventure story is, look it up or ask me. You need to have 2 conditional statements that are nested together. So for example you could have a paragraph of story and then an A or B choice, each one would lead to another paragraph and another choice. You also need to include endings. Be creative. Do not have more than the 2 nested if statements. Include test runs showing every ending of your story. A great example of an adventure story is at Link: https://xkcd.com/1350/
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 do the whole nested statements all 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/shellcond.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