CST 8177 - Lab 10
Working with variables
Student Name |
Student number |
Lab section: |
|
|
|
Objective
To practice problem-solving skills with several small scripts and their PDL, Data Dictionary (where necessary), and so on. Note: Print out your finished PDL, scripts , and test output to include in your Lab Book.
In-Lab Demo-- Demo one script from this lab, explaining its behaviour.
Working with variables
Note: For each script, you need to first write PDL and a brief Test Plan before you begin to write the bash commands.
Part 1: Sourcing a variable from a configuration file
Create a short script that sources a variable from a configuration file, first writing the PDL and then the bash commands.
Login as root.
Create a directory called config.d in your home directory and cd into it.
Create a file called sourcit.conf and enter the following line:
INIT=/etc/init
Create a bash shell script and name it sourcit.
Within the script, in this order:
Create an INIT variable and give it the value "/no-such-directory"
Display the contents of your variable INIT to the screen.
Include (source) the variable from the config file sourcit.conf
source ./sourcit.conf or . ./sourcit.conf
Display the contents of your variable INIT to the screen again. Did it change its value? [ Y / N ]
Check that the directory in INIT exists, using the variable, not the filename, to check for its existence.
Record the test you use: ____________________________________
If it doesn't exist, display an informative error message and exit with a return status of 1.
If it does exist, count all the lines in the files rcS*.conf there (using the variable again) that mention runlevel, but not as a shell variable. Then exit your script with an exit status of 0.
Test it thoroughly, with good input and bad.
Part 2: Updating a variable
Create a short script that sources a variable from a configuration file and adds the content to the PATH. As before, write the PDL first and then the bash script.
Login as root unless you are already root.
Create a short bash shell script in the same directory as above (config.d) and name it updpath. Create a config file as above named path.config with a variable having the initial value /root/bin. If the directory does not yet exist, create it before proceeding.
Check if the directory /root/bin already exists in the PATH. To do this, display the content of the PATH variable (echo $variable) and pipe it to the grep command. Be sure to use your sourced variable - do not hard-code the directory name in the script.
Record the command line you use: __________________________________________
This should not be successful until after you have run the script once.
Note: Suppress the grep output from your script; don't clutter the screen with the output of grep.
Copy the current PATH into a local script variable
Add the directory /root/bin to the end of the path from your variable (note: do not use ~/bin unless you take care to replace the '~' with the value from the $HOME variable).
Record the command line you use: __________________________________________
Display the updated path and the original. Ask the user if it's OK to change to the new path. If the reply is Y or Yes, replace the old path with the newly created path; otherwise, do not. Issue a meaningful message to the screen in both cases.
Test it thoroughly, with good input and bad.