Updated: 2015-09-06 00:56 EDT

1 Due Date and Deliverables

Do not print this assignment on paper!

2 Purpose of this Assignment

Do not print this assignment on paper! On paper, you cannot follow any of the hyperlink URLs that lead you to hints and course notes relevant to answering a question.

  1. Create shell scripts that deal with parameters and flow control.
  2. Practise with a text editor.

3 Introduction and Overview

This is an overview of how you are expected to complete this assignment. Read all the words before you start working.

For full marks, follow these directions exactly.

  1. Complete the Tasks listed below.
  2. Verify your own work before running the Checking Program.
  3. Run the Checking Program to help you find errors.
  4. Submit the output of the Checking Program to Blackboard before the due date.
  5. READ ALL THE WORDS to work effectively and not waste time.

Each of the tasks below (except the first) asks you to write a small executable shell script, based on the lecture notes and slides. None of the scripts need command pipelines (“|”) or Boolean expressions (“||” or “&&” or -a or -o); they are all simple scripts with simple conditional logic.

Each script should begin with the Standard Script Header you used for your previous script assignments. When you have completed each script, ensure that it is executable, so that it can be run as ./scriptname.sh.

Run the given tests on your scripts to make sure they work. Sample output for each of the scripts is given, so that you may check your work as you proceed. Make sure your script handles all of the sample inputs given, especially the inputs containing shell metacharacters. (System crackers often attack your system using special characters as input.)

You can use a Checking Program to check your work as you do the tasks. You can check your work with the checking program as often as you like before you submit your final mark. (Some tasks sections below require you to finish the whole section before running the checking program; you may not always be able to run the checking program successfully after every single task step.)

Since I also do manual marking of student assignments, your final mark may not be the same as the mark submitted using the current version of the Checking Program. I do not guarantee that any version of the Checking Program will find all the errors in your work. Complete your assignments according to the specifications, not according to the incomplete set of the mistakes detected by the Checking Program.

3.1 Save your work

You will create file system structure in your HOME directory on the CLS, with various directories, files, and links. When you are finished the tasks, leave these files, directories, and links in place as part of your deliverables on the CLS. Do not delete any assignment work until after the term is over! Assignments may be re-marked at any time; you must have your term work available right until term end.

3.2 The Source Directory

All references to the “Source Directory” below are to the CLS directory ~idallen/cst8177/14f/assignment06/ and that name starts with a tilde character ~ followed by a userid with no intervening slash. The leading tilde indicates to the shell that the pathname starts with the HOME directory of the account idallen (seven letters).

3.3 Searching the course notes

The previous term’s course notes are available on the Internet here: CST8207 GNU/Linux Operating Systems I. All the notes files are also on the CLS. You can learn about how to read and search these files using the command line on the CLS under the heading Copies of the CST8207 course notes near the bottom of the page Course Linux Server.

4 Tasks

4.1 Set Up – The Base Directory on the CLS

  1. Do a Remote Login to the Course Linux Server (CLS) from any existing computer, using the host name appropriate for whether you are on-campus or off-campus. All work in this assignment must be done on the CLS.

  2. Make an assignment06 directory in the same directory as you made assignment02 in a previous assignment.

This assignment06 directory is the Base Directory for most pathnames in this assignment. Store your files and answers in this Base Directory.

  1. Follow the instructions in the first two steps at the start of Checking Program below to create a working symbolic link to the executable Checking Program.

Check your work so far using the checking program symlink.

4.2 Basic Scripts

  1. Arguments on the command line and positional parameters:
    Create a two-line (plus header) script named arguments.sh that prints to the screen (standard output) these two things:
    1. the number of arguments given to the script
    2. all the arguments themselves, preserving blanks

    Examples:

    $ ./arguments.sh
    The number of arguments passed is: 0
    The arguments are:
    
    $ ./arguments.sh one   two    'three      four'   '*'
    The number of arguments passed is: 4
    The arguments are: one two three      four *
    
    $ ./arguments.sh foo bar >out
    $ cat out
    The number of arguments passed is: 2
    The arguments are: foo bar
    
    $ ./arguments.sh /bin/* >out
    $ head -n 1 out
    The number of arguments passed is: 151      (number may differ)
    
    $ ./arguments.sh /usr/bin/* >out
    $ head -n 1 out
    The number of arguments passed is: 3491     (number may differ)

    Note that GLOB characters do not expand when output by the script.

    Check your work so far using the checking program symlink.

  2. Reading input from the user/stdin:
    Create a three- or four-line (plus header) script named user_input.sh that prompts (on standard error) for input and reads one line of input from the user and then displays that input back to the user on standard output. The prompt may or may not end in a newline. Examples:

    $ ./user_input.sh
    Enter input:                       (may not end in newline)
    foo bar
    You entered:
    foo bar
    
    $ ./user_input.sh >out
    Enter input:                       (may not end in newline)
    *
    $ cat out
    You entered:
    *
    
    $ echo a b c | ./user_input.sh
    Enter input:                       (this line may not appear)
    You entered:
    a b c

    Note that GLOB characters do not expand when output by the script.

    Hint: The shell read command can be made to prompt for input (man bash), and it correctly displays the prompt on standard error. You can also use the shell syntax described in Good Error Message below to echo a message to standard error. Prompts must never disappear into the output file when standard output is redirected!

    Check your work so far using the checking program symlink.

  3. Conditional statements if then else, and test:
    Create a script named path_test.sh that outputs a line saying whether a pathname (any kind of pathname) exists or not. The pathname will be passed to the script as the only argument to the script. The script must ensure that exactly one argument is supplied. If the argument count is wrong, the script will issue both a Good Error Message and a Usage Message (how to use the script) on stderr and exit with a bad status of 2. The script will exit with a good status of 0 if the pathname exists and a bad status of 1 if the pathname does not exist.

    The script must have the following structure and use full if then else statements and not conditional operators such as &&:

    if the number of arguments is not 1,
        print a Good Error Message (see notes)
        print a Usage Message (how to use this script) on stderr
        exit the script with a status 2
    
    if the argument is a pathname that exists, then
        print a statement that the pathname 'xxx' exists
        exit the script with status 0
    else
       print a statement that the pathname 'xxx' doesn't exist
       exit the script with status 1

    where xxx is whatever argument the user supplied on the command line.

    Examples:

    $ ./path_test.sh
    ./path_test.sh: Expecting one pathname argument; found 0 ()
    Usage: ./path_test.sh pathname
    $ echo $?
    2
    
    $ ./path_test.sh a '*' c >out
    ./path_test.sh: Expecting one pathname argument; found 3 (a * c)
    Usage: ./path_test.sh pathname
    $ echo $?
    2
    
    $ ./path_test.sh path_test.sh
    Pathname exists: path_test.sh
    $ echo $?
    0
    
    $ ./path_test.sh ..
    Pathname exists: ..
    $ echo $?
    0
    
    $ ./path_test.sh /dev/null
    Pathname exists: /dev/null
    $ echo $?
    0
    
    $ ./path_test.sh /dev/sda
    Pathname exists: /dev/sda
    $ echo $?
    0
    
    $ ./path_test.sh /dev/log
    Pathname exists: /dev/log
    $ echo $?
    0
    
    $ ./path_test.sh nosuchfile
    Pathname nonexistent: nosuchfile
    $ echo $?
    1
    
    $ ./path_test.sh '*' >out
    $ echo $?
    1
    $ cat out
    Pathname nonexistent: *

    Note that GLOB characters do not expand when output by the script. Make sure you know how to write a Good Error Message to stderr.

    Check your work so far using the checking program symlink.

  4. Loop statements while and test:
    Combine elements from the previous two scripts to create a script named path_test_loop.sh that is a looping version of the previous path_test.sh script. The new script will read pathname input from the user instead of using command line arguments.

    Copy the previous path_test.sh script to path_test_loop.sh. Modify the new script to generate a Good Error Message and Usage Message on stderr if any arguments are found on the command line. If there are any arguments, the script should state that it must be run without any arguments and exit with status 2.

    Create a while loop that prompts and reads input from the user (until EOF) and then uses the input as a pathname to test that write permission is granted (man test). (Re-use parts of the previous scripts.) You will need an option to the shell read statement that issues a prompt to the user (man bash).

    The script must have the following structure and use full if then else statements and not conditional operators such as &&:

    while the script prompts and reads input from the user successfully
        if the input from the user is a pathname with write permission
            print a statement that the pathname 'xxx' is writable
        else
            print a statement that the pathname 'xxx' does not
               exist or is not writable

    Examples:

    $ ./path_test_loop.sh  foo  bar
    ./path_test_loop.sh: Expecting no arguments; found 2 (foo bar)
    Usage: ./path_test_loop.sh
    $ echo $?
    2
    
    $ ./path_test_loop.sh  x  y  z  >out
    ./path_test_loop.sh: Expecting no arguments; found 3 (x y z)
    Usage: ./path_test_loop.sh
    $ echo $?
    2
    
    $ ./path_test_loop.sh
    Enter pathname: path_test_loop.sh
    Pathname is writable: path_test_loop.sh
    Enter pathname: .
    Pathname is writable: .
    Enter pathname: ../..
    Pathname is writable: ../..
    Enter pathname: nosuchfile
    Pathname is nonexistent or not writable: nosuchfile
    Enter pathname: /
    Pathname is nonexistent or not writable: /
    Enter pathname: foo bar
    Pathname is nonexistent or not writable: foo bar
    Enter pathname: *
    Pathname is nonexistent or not writable: *
    Enter pathname: ^D           (signal CTRL-D EOF to end the script)
    $
    
    $ ./path_test_loop.sh >out
    Enter pathname: out
    Enter pathname: ***
    Enter pathname: ^D           (signal CTRL-D EOF to end the script)
    $ cat out
    Pathname is writable: out
    Pathname is nonexistent or not writable: ***
    $
    
    $ echo "." | ./path_test_loop.sh
    Pathname is writable: .
    $
    
    $ ./path_test_loop.sh </dev/null
    $

    You need an option to the shell read statement that issues a prompt to the user (man bash). Note that GLOB characters do not expand when output by the script. Make sure the script exits on EOF.

    Check your work so far using the checking program symlink.

4.3 awk wrapper script: extracting a column of input

Recall in lecture that we used used the awk program to extract the first space-delimited column of an input stream. We will develop a script named acol (Awk COLumn) that extracts any column of input.

  1. Create a one-line (plus header) script named acol1.sh that uses awk to read its standard input and extract the first column, exactly as used in the lecture notes. Example usage:

    $ echo a b c | ./acol1.sh
    a
    
    $ date | ./acol1.sh
    Wed
    
    $ last | ./acol1.sh
    idallen
    idallen
    kelleyt
    donnelr
    [...etc...]

    Check your work so far using the checking program symlink.

  2. Create a one-line (plus header) script named acol2.sh that uses awk to read its standard input and extract the second column of input. Example usage:

    $ echo a b c | ./acol2.sh
    b
    
    $ date | ./acol1.sh
    Feb
    
    $ last | ./acol2.sh
    pts/9
    pts/14
    pts/50
    pts/50
    [...etc...]

    Check your work so far using the checking program symlink.

  3. Create a one-line (plus header) script named acolNF.sh that uses awk to read its standard input and extract the last (NF) column of input. Example usage:

    $ echo a b c | ./acolNF.sh
    c
    
    $ echo a b d e f g h i j | ./acolNF.sh
    j
    
    $ date | ./acolNF.sh
    2013
    
    $ last | ./acolNF.sh
    (00:31)
    (02:11)
    (02:02)
    (00:01)
    [...etc...]

    Check your work so far using the checking program symlink.

It should be clear that having a separate script for every possible number of columns is not a good thing. Let’s write one script that takes as its only argument the column number we want awk to print.

  1. Create a one-line (plus header) script named acolnew.sh that uses awk to read its standard input and extract the column of input given as an argument on the command line. This script is a simple modification of your previous script; you only need to change about a half-dozen characters to make it work. Example usage:

    $ echo a b c d e f | ./acolnew.sh 1
    a
    
    $ echo a b c d e f | ./acolnew.sh 2
    b
    
    $ echo a b c d e f | ./acolnew.sh NF
    f

    The script takes a single argument that is either a number (e.g. 1, 2, etc.) or the string NF and substitutes that argument directly into the awk command line as the column to print.

    (Hint: Instead of hard-coding the column number in the awk command line, as you did in the the above three previous scripts, use the first argument variable instead. You may need to adjust the Quoting around the awk command arguments to allow the argument variable to be expanded by the shell but not the $ used by awk to select the column number. You cannot use the awk -v option here.)

    The script is a simple modification of your previous script; it is one line (plus header); it does not do any input checking or argument processing. If you don’t supply an argument, or you pass something that awk doesn’t understand, you will get an error from awk – this is okay:

    $ echo a b c d e f | ./acolnew.sh
    ./acolnew.sh: 8: ./acolnew.sh: 1: parameter not set
    
    $ echo a b c d e f | ./acolnew.sh ''
    awk: { print $ }
    awk:           ^ syntax error
    
    $ echo a b c d e f | ./acolnew.sh ' '
    awk: { print $  }
    awk:            ^ syntax error
    
    $ echo a b c d e f | ./acolnew.sh @
    awk: { print $@ }
    awk:          ^ invalid char '@' in expression

    If you supply more than one argument, the script won’t detect the error and will simply ignore the extra arguments – this is okay:

    $ echo a b c | ./acolnew.sh NF these arguments are ignored
    c

    This one-line script (plus header) does not validate its input, so it gives cryptic errors if the argument is incorrect and it does not warn you if you give too many arguments. This is not a production-quality script. We will fix it in the next step.

    Check your work so far using the checking program symlink.

  2. Copy acolnew.sh to acol.sh and add input validation to the new script. The new script must check to make sure it has exactly one input argument and do minimal validation of that one argument before using it with awk to print the given column number. You do not need to change the awk line at all. You only need to add input validation to the script, before calling awk.

    Your script should enforce the “only one argument” requirement. Print both a Good Error Message and Usage Message on stderr and exit with a bad error status if the number of arguments is not exactly one, or if the one argument is equal to the empty string (''), or equal to a single blank character (' ').

    You are not required to validate that the argument is a number or the string NF, since that kind of pattern-matching needs more advanced scripting knowledge.

    Examples:

    $ echo a b c | ./acol.sh
    ./acol.sh: Expecting one column number argument; found 0 ()
    Usage: ./acol.sh colnum
    $ echo $?
    2
    
    $ echo a b c | ./acol.sh 1 2 3 a b c >out
    ./acol.sh: Expecting one column number argument; found 6 (1 2 3 a b c)
    Usage: ./acol.sh colnum
    $ echo $?
    2
    
    $ echo a b c | ./acol.sh ''
    ./acol.sh: column number argument is empty
    Usage: ./acol.sh colnum
    $ echo $?
    2
    
    $ echo a b c | ./acol.sh ' '
    ./acol.sh: column number argument is a blank (space)
    Usage: ./acol.sh colnum
    $ echo $?
    2
    
    $ echo one,two three,four | ./acol.sh NF
    three,four
    $ echo $?
    0

    Check your work so far using the checking program symlink.

  3. Link acol.sh into your personal bin/ directory using the name acol and use it whenever you need to see just one column of data. Take your acol script with you to your next job!

    $ last | acol 1 | sort | uniq -c | sort -nr | head -5
    93 kelleyt
    90 user1234
    87 idallen
    82 user2345
    77 donn0067
    
    $ last | acol 2 | sort | uniq -c | sort -nr | head -5
    450 pts/2
    403 pts/3
    328 pts/5
    235 pts/11
    226 pts/8

    Check your work so far using the checking program symlink.

4.4 When you are done

That is all the tasks you need to do.

Check your work a final time using the Checking Program and save the output as described below. Submit your mark following the directions below.

5 Good Error Messages

Good shell script error messages must obey these four rules:

  1. Error messages must appear on standard error, not standard output. You can use the shell syntax 1>&2 to send to stderr any output normally destined for stdout. See the examples below.
    • Usually 1>&2 is used on echo statements, to send the text to standard error instead of standard output.
  2. Must contain the name of the program that is issuing the message (from $0).
    • Do not put the name of the script into the script; always use $0.
  3. Must state what kind of input was expected (e.g. expecting one file name).
    • Do not say only “expecting one argument”, since that doesn’t say what kind of argument is needed. Be explicit.
  4. Must display what the user actually entered (e.g. found 3 (a b c)
    • Display both the number of arguments and their values.

Never say just missing argument or illegal input or invalid input or too many. Always specify what is needed and how many is “too many” or “too few”:

echo 1>&2 "$0: Expecting 3 file names; found $# ($*)"
echo 1>&2 "$0: Student age $student_age is not between $min_age and $max_age"
echo 1>&2 "$0: Modify days $moddays is less than zero"

After detecting an error, the usual thing to do is to exit the script with a non-zero return code. Don’t keep processing bad data!

6 Checking, Marking, and Submitting your Work

Summary: Do some tasks, then run the checking program to verify your work as you go. You can run the checking program as often as you want. When you have the best mark, upload the marks file to Blackboard.

Since I also do manual marking of student assignments, your final mark may not be the same as the mark submitted using the current version of the Checking Program. I do not guarantee that any version of the Checking Program will find all the errors in your work. Complete your assignments according to the specifications, not according to the incomplete set of the mistakes detected by the Checking Program.

  1. There is a Checking Program named assignment06check in the Source Directory on the CLS. Create a [Symbolic Link] to this program named check under your new Base Directory on the CLS so that you can easily run the program to check your work and assign your work a mark on the CLS. Note: You can create a symbolic link to this executable program but you do not have permission to read or copy the program file.

  2. Execute the above check program on the CLS using its symbolic link. (Review the Search Path notes if you forget how to run a program by pathname from the command line.) This program will check your work, assign you a mark, and display the output on your screen. (You may want to paginate the long output so you can read all of it.)

    You may run the check program as many times as you wish, to correct mistakes and get the best mark. Some task sections require you to finish the whole section before running the checking program at the end; you may not always be able to run the checking program successfully after every single task step.

  3. When you are done with checking this assignment, and you like what you see on your screen, redirect the output of the Checking Program into the text file assignment06.txt under your Base Directory on the CLS. Use that exact name. Case (upper/lower case letters) matters. Be absolutely accurate, as if your marks depended on it.
    • Do not edit the output file. Submit it exactly as given.
    • Make sure the file actually contains the output of the checking program!
    • The file should contain near the bottom a line starting with: YOUR MARK for
    • Really! MAKE SURE THE FILE HAS YOUR MARKS IN IT!
  4. Transfer the above assignment06.txt file from the CLS to your local computer and verify that the file still contains all the output from the checking program. Do not edit this file! No empty files, please! Edited or damaged files will not be marked. You may want to refer to your File Transfer notes.
    • Do not edit the output file. Submit it exactly as given.
    • Make sure the file actually contains the output of the checking program!
    • The file should contain near the bottom a line starting with: YOUR MARK for
    • Really! MAKE SURE THE FILE HAS YOUR MARKS IN IT!
  5. Upload the assignment06.txt file from your local computer to the correct Assignment area on Blackboard (with the exact name) before the due date:
    1. On your local computer use a web browser to log in to Blackboard and go to the Blackboard page for this course.
    2. Go to the Blackboard Assignments area for the course, in the left side-bar menu, and find the current assignment.
    3. Under Assignments, click on the underlined assignment06 link for this assignment.
      1. If this is your first upload, the Upload Assignment page will open directly; skip the next sentence.
      2. If you have already uploaded previously, the Review Submission History page will be open and you must use the Start New button at the bottom of the page to get to the Upload Assignment page.
    4. On the Upload Assignment page, scroll down and beside Attach File use Browse My Computer to find and attach your assignment file from your local computer. Make sure the assignment file has the correct name on your local computer before you attach it.
    5. After you have attached the file on the Upload Assignment page, scroll down to the bottom of the page and use the Submit button to actually upload your attached assignment file to Blackboard.

    Use only Attach File on the Upload Assignment page. Do not enter any text into the Text Submission or Comments boxes on Blackboard; I do not read them. Use only the Attach File section followed by the Submit button. If you need to comment on any assignment submission, send me email.

    You can revise and upload the file more than once using the Start New button on the Review Submission History page to open a new Upload Assignment page. I only look at the most recent submission.

    You must upload the file with the correct name from your local computer; you cannot correct the name as you upload it to Blackboard.

  6. Verify that Blackboard has received your submission: After using the Submit button, you will see a page titled Review Submission History that will show all your uploaded submissions for this assignment. Each of your submissions is called an Attempt on this page. A drop-down list of all your attempts is available.
    1. Verify that your latest Attempt has the correct 16-character, lower-case file name under the SUBMISSION heading.
    2. The one file name must be the only thing under the SUBMISSION heading. Only the one file name is allowed.
    3. No COMMENTS heading should be visible on the page. Do not enter any comments when you upload an assignment.
    4. Save a screen capture of the Review Submission History page on your local computer, showing the single uploaded file name listed under SUBMISSION. If you want to claim that you uploaded the file and Blackboard lost it, you will need this screen capture to prove that you actually uploaded the file. (To date, Blackboard has never lost an uploaded file.)

    You will also see the Review Submission History page any time you already have an assignment attempt uploaded and you click on the underlined assignment06 link. You can use the Start New button on this page to re-upload your assignment as many times as you like.

    You cannot delete an assignment attempt, but you can always upload a new version. I only mark the latest version.

  7. Your instructor may also mark files in your directory in your CLS account after the due date. Leave everything there on the CLS. Do not delete any assignment work from the CLS until after the term is over!

READ ALL THE WORDS. OH PLEASE, PLEASE, PLEASE READ ALL THE WORDS!

Author: 
| Wenjuan Jiang, Todd Kelley, and
| Ian! D. Allen  -  idallen@idallen.ca  -  Ottawa, Ontario, Canada
| Home Page: http://idallen.com/   Contact Improv: http://contactimprov.ca/
| College professor (Free/Libre GNU+Linux) at: http://teaching.idallen.com/
| Defend digital freedom:  http://eff.org/  and have fun:  http://fools.ca/

Plain Text - plain text version of this page in Pandoc Markdown format

Campaign for non-browser-specific HTML   Valid XHTML 1.0 Transitional   Valid CSS!   Creative Commons by nc sa 3.0   Hacker Ideals Emblem   Author Ian! D. Allen