Questions in this exercise requires you to turn in scripts. Instructors/TAs upon their discretion may ask you to demonstrate/explain your solution. No points are awarded for commands that do not execute at all or programs that do not compile in mimi. (Commands/programs that execute/compile, but provide incorrect behavior/output will be given partial marks.) All questions are graded proportionally. This means that if 40% of the question is correct, you will receive 40% of the grade.
Please read through the entire assignment before you start working on it. You can loose up to 3 points for not following the instructions.
Labs A through D will provide some background help for this mini assignment.
Unless otherwise stated, all the names of the scripts and programs that you write, commands, options, input arguments, etc. are implied to be case-sensitive.
For this question you will need to:
- Determine which login script your shell uses when you ssh or putty.
- Modify your login script as specified below.
- Comment the login script with your changes.
- Upload the login script as part of your assignment submission.
Modify your login script in all the following ways:
- Display Welcome to [hostname]! Here you should replace [hostname] with the name of the host to which you login (explore the hostname command and see how you can incorporate it’s output into this message).
- List the total number of sessions in which you are logged into that particular host (Explore a combination of who, grep and wc commands to accomplish this task). The message should be You have
<X>login sessions to this host. Replace
<X>with actual number of sessions that you have logged into that system.
- Alias comp206 so that typing it executes cd /Projects/COMP206 (the folder you created in mini 1 assignment).
- Set your shell’s prompt to include your user name, host name, current directory and time (all dynamic - see sample screenshot below for an expected outcome).
- Execute the fortune command to display any random quote.
- Plus one other thing of your liking (make sure to comment this one for the TA). (1 Point) for writing comments on the script describing the intentions of respective commands/statements.
Below is an expected screen shot of what happens when you login. In this instance, I logged in and got a prompt which has time included in it. I executed the pwd and cd commands only to show that the next prompt that the shell gives has the time and directory automatically updated. You do not have to do execute these commands in your updated profile.
I recommend you start out by implementing/testing each idea in the shell prompt first before making changes to your actual login script.
For this exercise, you should turn in your modified login script.
In this exercise, you will learn how to use a set of environment variables that are setup in a “configuration script”, in your main program script, so that your main script can be run without modification by users of your program. You have been provided with a configuration script a3config that is a simple text file which contains the environment variables that we will be using. The contents of this file is shown below for discussion. You can use vi to edit this file.
$ cat a3config # You may edit this file for your testing. # You should only change the values of the variables for the purpose of your testing # However, TAs will use their own a3config file to test your script with. # Therefore, do not add any other code here that is required for your script's execution. DIRNAME=/tmp/__206__$LOGNAME/mydir EXTENSION=msg SHOW=true
Your task is to write a script finder.sh that will check if there are files in the directory whose value is stored in the variable DIRNAME such that the files have the extension as indicated in the variable EXTENSION. If there are such files, you should print a list of them. Further, if the variable SHOW has the value true, the contents of the file must be displayed to screen.
In order to accomplish this, you will have to execute the contents of a3config within finder.sh. Explore the source or dot (.) commands to accomplish this.
If your finder.sh script cannot find a3config, then it should throw an error message and terminate the script with a non zero exit code.
$ ./finder.sh Error cannot find a3config $ echo $? 1
The echo $? command is included above to merely show you the exit code from the script was non zero.
If either of the variables DIRNAME or EXTENSION is not present in a3config or if their values are empty, the script should display an error message and terminate with a non zero exit code.
$ ./finder.sh Error DIRNAME and EXTENSION must be set $ echo $? 2
If the directory mentioned in DIRNAME does not exist, then the script should throw an error message and terminate with a non zero exit code.
$ ./finder.sh Error directory /nosuchdir/dir1 does not exist $ echo $? 3
If the script cannot locate any files in the directory that have the expected extension, then it should display the following message, but still terminate with a zero exit code.
$ ./finder.sh Unable to locate any files with extension msg in /etc $ echo $? 0
If the script is able to locate files in the directory, and the value of SHOW is true then it should list the names of the files, along with the contents of each file and terminate with a zero exit code.
If the script is able to locate files in the directory, but the value of SHOW is not set to true or if the SHOW variable itself is missing from a3config, then it should list the names of the files, but do not display the contents of files and terminate with a zero exit code.
- Your finder.sh must be executable using the bash shell in mimi.
- For properly commenting throughout the source code.
- Detecting and displaying an error message and terminating with non zero exit code when the script cannot find a3config.
- Detecting and displaying an error message and terminating with non zero exit code when any of the variables DIRNAME and EXTENSION are not present a3config or is empty.
- Displaying an error message and terminating with non zero exit code when the directory does not exist.
- Displaying a message and terminating with zero exit code when the directory does exist but cannont locate any files with the expected extension.
- Displaying only a list of files (not contents) and terminating with zero exit code when the directory does exist and is able to locate files with the expected extension, however the variable SHOW is not set in a3config or is not set to the value true.
- Displaying a list of files and their contents and terminating with zero exit code when the directory does exist and is able to locate files with the expected extension, and the variable SHOW is set in a3config to the value true.
For this exercise, you should turn in the finder.sh script that you wrote.
In this exercise, you will update the script showusr that is given to you (you can use vi to edit this file), to write a function fname, that accepts a userid (such as your socs unix account) as the argument and sets an environment variable FNAME with the value that is the first name of the userid (explore the pinky command to get the first name of the userid). The function will also return an integer code to indicate its status (see examples below). Do not use the exit command in the function !
- For properly commenting throughout the source code.
- If fname is not passed an argument, set the variable FNAME to ERROR and return a value of 1. Below is an example of me “loading” the function to shell environment and then executing it (with no argument). I am using the echo command to check the return code as well as the value stored in FNAME.
- if the argument passed to fname cannot be found among the sessions logged in, it should set FNAME to NOTFOUND and return 0.
- if the userid passed as argument to fname is found among the sessions logged in, it should set FNAME to the first name of that user and return 0.
You should turn in your modified showusr script.
Turn in the modified login script, and the scripts finders.sh, and showusr named properly (3 items). You do not have to zip all of the files together. The files must be uploaded to mycourses under assignment 3. DO NOT turn in a3config. TAs will use their own a3config.
There is a tester script mini3tester.sh that is provided with the assignment that you can use to test how your finder.sh and showusr is behaving. TAs will be using the exact same tester script to grade your assignment. You can test your modified login script by doing a login to mimi as usual.
It is recommended that when you start writing your program, test it yourself first with the above examples. Once you are fairly confident that your program is working, you can test it using the tester script.
You can compare the output produced by running the mini tester on your scripts to that produced by the mini tester on the solution which is given in mini3tester.out.txt.
You have to implement only the scenarios discussed in the above examples and as required to pass the tester script given to you. You are free to explore for sophisticated approaches, but not obliged to.
The following discussion is meant to encourage you to search independently for creative and optimal ways to perform rudimentary tasks with less effort and/or make your program robust and sophisticated. It does not impact the points that you can achieve in the above questions.
- The command pinky can be used to find a person’s information if they have a session running in the host. Can you find another command that can be used for the same purpose, even if the person is not logged into the host?