- OPS435 -

OPEN SYSTEM AUTOMATION

Lab #8

FOCUS: Regular Expressions / Trapping Signals

PART A - Perform the following tasks:

  1. Logon to your Matrix account.
  2. Perform the week8 online tutorial called "Online Linux Tutorials". This tutorial must be completed to count as half of the mark for this lab #8. The tutorial, when properly completed will send your instructor and yourself an e-mail message to verify that the tutorial was completed.

    Here is a link to the online tutorial instructions: [ html ]

  3. Proceed to Part B
  4. PART B - Perform the following tasks:

    1. Logon to your Matrix account. This lab requires that you use your Matrix account in order to submit your lab.
    2. Make an empty directory, called lab08.
    3. Use the cd command to make lab08 your present working directory.
    4. Copy and paste the contents of the link below into a file called numbers.txt (in your lab08 directory).

      Here is the Link: numbers.txt

    5. Use a text editor to create a file called lab08.bash and add a shebang line to make it a portable Bash shell script.
    6. Add the following lines displayed below to your existing shell script:


      if [ $# -ne 1 ]
      then
      echo "USAGE: $0 [textfile]" >&2
      exit 1
      fi

      if [ ! -f $1 ]
      then
      echo "ERROR: File pathname \"$1\" does not exist"
      exit 1
      fi

      echo "CONTENTS OF $1:"
      cat $1
      echo
      echo "JUST NUMBERS:"
      echo
      grep "^[0-9][0-9]*$" $1


    7. Save and exit your file called lab08.bash, set execute permissions for your newly-created file lab08.bash.
    8. Run your shell script by issuing the following:

      ./lab08.bash numbers.txt

    9. Take a moment to notice what your shell script did...
    10. Copy and paste the contents of the link below into a file called numbers2.txt (in your lab08 directory).

      Here is the Link: numbers2.txt

    11. Run your shell script again, but as shown below:

      ./lab08.bash numbers2.txt

    12. You should notice that only whole numbers are displayed. Any numbers that start with a positive (+) or negative (-) sign are NOT displayed. Also, decimal numbers are NOT displayed as well...
    13. Change the last command in your shell script (i.e. the "grep" command) to read:

      grep "^[+-]*[0-9]*[.]*[0-9]*$" $1


    14. Save your work and run your shell script with the same argument as in step #10, and notice the results.
    15. You should notice that although it now selects numbers that start with "+" or "-", and contain decimals, it also displays multiple occurences of "+", "-", and "."

      In order for solve this problem, we need to use "extended" regular expressions (and use the "egrep" command or the "grep" command with an arguement to properly use "extended" regular expressions)...

    16. Change the last command (i.e. the "grep" command) in your shell script to read:

      egrep "^[+-]?[0-9]+[.]?[0-9]+$" $1


    17. Save your changes and run your shell script as shown in step #10, and notice the results...
    18. Using a text editor, copy and paste the script displayed below into separate shell scripts to run and see what happens.

      NOTE: You will need to view the comments INSIDE the shell script in order to know what to do when the shell script is running...
      Here are the Links to the shell scripts:

      [trap1a.bash]
      [trap1b.bash]
      [trap2.bash]
      [trap3.bash]
      [trap14.bash]
      [trap15.bash]


    19. Save those files, set execute permissions, and then run them (one-at-at time) to see how shell scripts can "trap" signals...
    20. Proceed to Part C

    PART C - Write a shell script
    1. Make certain that you are currently located in the lab08 directory
    2. Create a portable bash shell script called interrupt.bash
    3. Make certain that your shell script contains the appropriate shebang line.
    4. The shell script should only accept one argument AFTER your shell script. If the user enters zero, or more than 1 argument after your shell script, then you will display a STDERR message, and terminate the script with a FALSE value.
    5. Your shell script then should check to see if that argument after your command represents a valid file pathname. If it it already exists as a regular file, then your shell script will display a STDERR message, and terminate the script with a FALSE value.
    6. If the pathname does not exist, then create it (hint: use "touch" command).
    7. Then your shell script will prompt the user to append the contents of your shell script to that valid pathname argument. AT THIS POINT, IF YOU PRESS <CTRL><c> then your shell script will trap that signal and run a function called "cleanup" that will then perform "clean-up" operations and then exit the shell script with a TRUE value.

      Refer to the SAMPLE RUNS below:

      ./interrupt.bash
      USAGE: ./interrupt.bash [filepathname]
      ./interrupt.bash a
      ERROR: The filename "a" already exists
      ./interrupt.bash b
      Add listing to bottom of "b"? # <-- user presses <ctrl><c>

      ATTENTION:

      You pressed the key combination <CTRL><c>

      Do you want to remove the temporary file "b"? y
      ls b
      ls: cannot access b: No such file or directory

    8. Save your changes, and run your shell script to make certain it works...
    9. Proceed to Part D to submit your lab...

    PART D - Submit your Lab #8:
    1. Make certain that you are currently located in the lab08 directory
    2. Issue the following Bash Shell script to check and if correct, send e-mail to your OPS435 professor (works only for Murray Saul's sections):

      /home/murray.saul/labs/submit-lab8.bash

      This shell script should either indicate problems and hints for you to correct your shell script, or indicate that your lab #8 submission was successful. If your lab was successfully submitted, both you and your OPS435 professor (Murray Saul) should have received an e-mail message.

      Note: Keep this e-mail message for the remainder of the semester as date and time stamp proof that you submitted the lab in case there is a discrepancy with your OPS435 lab grade...