Lab 3 - Controlling execution in bash
The purpose of this lab is to develop skills in using, storing, and manipulating data in bash scripts.
This exercise gives some exposure to testing variable content.
- Download the checkMYVAR.sh script and do the following exercises to see how exporting variables can affect programs you run
wget -O ~/COMP2101/bash/checkMYVAR.sh https://zonzorp.github.io/COMP2101/Labs/bash/scripts-lab3/checkMYVAR.sh
chmod +x ~/COMP2101/bash/checkMYVAR.sh
- Run it without creating the MYVAR variable
- On your command line, create the MYVAR variable without putting anything in it, then run the script
- Put some data in the MYVAR variable, and try running the script again
MYVAR="some data"
checkMYVAR.sh
- Export the MYVAR variable, and try running the script again
export MYVAR
checkMYVAR.sh
- Remove the data from the MYVAR variable, and try running the script again
- Delete the MYVAR variable, and try running the script again
unset MYVAR
checkMYVAR.sh
- Create and export the MYVAR variable in a single command, and try running the script again
export MYVAR="some data"
checkMYVAR.sh
login-welcome
This exercise converts the example welcome script to one which displays its output in a more fun way, suitable for use in a login script.
- Use your welcome-message.sh script as a starting point by making a copy of of it called login-welcome.sh
cp ~/COMP2101/bash/welcome-message.sh ~/COMP2101/bash/login-welcome.sh
- Modify it to store the output in a variable instead of displaying it on the screen directly
- Use the cowsay command to display the variable containing your output
- Test your changed script to make sure it works before proceeding to the next step
vi ~/COMP2101/bash/login-welcome.sh
login-welcome.sh
- Modify your .bash_login file (create it if necessary) to run your login-welcome.sh script
- Test it to be sure it works, then test that it runs at login by logging in with ssh
vi ~/.bash_login
bash ~/.bash_login
ssh localhost
- Save the final version of your script to your github repository (stage, commit, push).
This exercise practices testing files, numbers, and strings.
- Download the tests.sh script
wget -O ~/COMP2101/bash/tests.sh https://zonzorp.github.io/COMP2101/Labs/bash/scripts-lab3/tests.sh
chmod +x ~/COMP2101/bash/tests.sh
- Run it to see what it does
- Modify the script according to the tasks in the script’s comments.
- Test your changes to be sure they work
vi ~/COMP2101/bash/tests.sh
tests.sh
This exercise practices looping and testing user input.
- Download the guessinggame.sh script
wget -O ~/COMP2101/bash/guessinggame.sh https://zonzorp.github.io/COMP2101/Labs/bash/scripts-lab3/guessinggame.sh
chmod +x ~/COMP2101/bash/guessinggame.sh
- Run it to see what it does
- Modify the script according to the tasks in the script’s comments.
- Test your changes to be sure they work
vi ~/COMP2101/bash/guessinggame.sh
guessinggame.sh
Challenge Script:
Further Improvements to sysinfo.sh
This lab will build on the sysinfo.sh script from the previous lab. Keeping all the main goals from the previous labs, your output should be displayed in sections, with titles and all data should be labeled. Only the required data should be present in the output.
In this lab, we are adding content, not so much changing what we are doing. Some of this content will require you to evaluate what output comes from the commands you are using instead of just printing it out.
Add sections to your report, so that the output is visually easier to use.
- Each section should have a title, and each section should be visually separate from the previous section for easy reading.
- Any data item which would be blank because data is not available at runtime should not be included in the output. If an entire section would be blank, your script must include an error message that tells the user that data for that section is unavailable.
- Most of the data for this report will come from the
lshw
command family, and possibly dmidecode
. They are not fast commands, so you should only run them once, saving their output in a variable which is then parsed for the needed output data for your report.
- These commands require root privilege, so your script should begin by checking if the user running it has root privilege and if they do not, it should display an error message telling the user they require root, and then exiting with a failure status.
- If any of your commands might fail, you should be checking if they do and printing appropriate error messages.
Report content
A section for system description containing:
- title
- Computer manufacturer
- Computer description or model
- Computer serial number
- title
- CPU manufacturer and model
- CPU architecture
- CPU core count
- CPU maximum speed in a human friendly format
- Sizes of caches (L1, L2, L3) in a human friendly format
- title
- Linux distro
- Distro version
Create, test, and save your script
- Create your script as described above.
- Test your script to be sure it works as required
vi ~/COMP2101/bash/sysinfo.sh
sysinfo.sh
- If anything didn’t work correctly, go back, fix the issues and repeat until it does work right.
- Save the final version of your script to your github repository (
git add
, git commit -m message
, git push
).
Grading
There is nothing to submit for this lab. This lab exists to create a useful script and learn to use some important commands. It will provide the basis for the Bash Assignment later in the course which does count towards your semester mark. Ask your professor for help to complete this lab before starting the next lesson in this course if you are unable to complete it on your own.
This document is copyright Dennis Simpson, 2013-2023.