COMP2101

Lab 3 - Controlling execution in bash

The purpose of this lab is to develop skills in using, storing, and manipulating data in bash scripts.

checkMYVAR.sh

This exercise gives some exposure to testing variable content.

  1. 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
    
  2. Run it without creating the MYVAR variable
     checkMYVAR.sh
    
  3. On your command line, create the MYVAR variable without putting anything in it, then run the script
     MYVAR=
     checkMYVAR.sh
    
  4. Put some data in the MYVAR variable, and try running the script again
     MYVAR="some data"
     checkMYVAR.sh
    
  5. Export the MYVAR variable, and try running the script again
     export MYVAR
     checkMYVAR.sh
    
  6. Remove the data from the MYVAR variable, and try running the script again
     MYVAR=
     checkMYVAR.sh
    
  7. Delete the MYVAR variable, and try running the script again
     unset MYVAR
     checkMYVAR.sh
    
  8. 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.

  1. 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
    
  2. Modify it to store the output in a variable instead of displaying it on the screen directly
  3. Use the cowsay command to display the variable containing your output
  4. Test your changed script to make sure it works before proceeding to the next step
    vi ~/COMP2101/bash/login-welcome.sh
    login-welcome.sh
    
  5. Modify your .bash_login file (create it if necessary) to run your login-welcome.sh script
  6. 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
    
  7. Save the final version of your script to your github repository (stage, commit, push).

tests.sh

This exercise practices testing files, numbers, and strings.

  1. 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
    
  2. Run it to see what it does
    tests.sh
    
  3. Modify the script according to the tasks in the script’s comments.
  4. Test your changes to be sure they work
    vi ~/COMP2101/bash/tests.sh
    tests.sh
    

guessinggame.sh

This exercise practices looping and testing user input.

  1. 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
    
  2. Run it to see what it does
    guessinggame.sh
    
  3. Modify the script according to the tasks in the script’s comments.
  4. 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.

Report content

A section for system description containing:

A section for CPU information containing:

A section for operating system information containing:

Create, test, and save your script

  1. Create your script as described above.
  2. Test your script to be sure it works as required
    vi ~/COMP2101/bash/sysinfo.sh
    sysinfo.sh
    
  3. If anything didn’t work correctly, go back, fix the issues and repeat until it does work right.
  4. 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.