- OPS435 -

OPEN SYSTEM AUTOMATION

Lab #7

FOCUS: Loops (Determinant, Indeterminant)

PART A - Perform the following tasks:

  1. Logon to your Matrix account.
  2. Perform the week7 online tutorial called "Online Linux Tutorials". This tutorial must be completed to count as half of the mark for this lab #7. 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 lab07.
    3. Use the cd command to make lab07 your present working directory.
    4. Use a text editor to create a file called lab07.bash and add a shebang line to make it a portable Bash shell script.
    5. Add the following lines displayed below to your existing shell script:


      for x in pens pencils erasers rulers
      do
          echo "\$x is: $x"
      done
      echo


    6. Save and exit your file called lab07.bash, set execute permissions for your newly-created file lab07.bash.
    7. Run your shell script and notice what happens.
    8. Add the following lines to the bottom of your shell script

      for x
      do
          echo "\$x is: $x"
      done
      echo


    9. Save your work,and run your shell script without any arguments. What happens?
    10. Run your shell script as displayed below:

      ./lab07.bash one two three

    11. What happened? What are the differences between the first and the second loop?
    12. Add the following lines of code at the bottom of your shell script:

      x=1
      while [ $x -le 5 ]
      do
      echo "this is loop #$x"
      x=$((x+1))
      done
      echo


      Try to understand in terms of the "while" loop what happened for each situation.
      What would happen if the command x=$((x+1)) was removed?

    13. Save your work and run your shell script without any arguments.
    14. Add the following lines to your shell script:

      read -p "Enter a valid integer: " num
      until echo $num | grep -s "^[-+]*[0-9][0-9]*$"
      do
      echo "You have selected an INVALID integer." >&2
      read -p "Enter a valid integer: " num
      done

      echo "You entered the integer: $num"


    15. Save your changes and run your shell script as shown in step #10.
      Try entering in some invalid and valid integers (like +12, -2, etc...).
    16. Proceed to Part C

    PART C - Write a shell script
    1. Make certain that you are currently located in the lab07 directory
    2. Create a portable bash shell script called getEvenNumber.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. Use a loop to keep prompting the user to enter a EVEN number if the argument was NOT an even number. Once an even number was entered (or the argument after script name was even), then the value of the even number is displayed. Hint: use a user-defined variable to represent $1. Refer to SAMPLE RUNS below:

      SAMPLE RUN (commands are in bold)

      ./getEvenNumber.bash
      USAGE: ./getEvenNumber.bash [number]
      ./getEvenNumber.bash 3
      Number is NOT even...
      Please enter an EVEN number: 4
      The EVEN number is: 4
      ./getEvenNumber.bash 8
      The EVEN number is: 8

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

    PART D - Submit your Lab #7:
    1. Make certain that you are currently located in the lab07 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-lab7.bash

      This shell script should either indicate problems and hints for you to correct your shell script, or indicate that your lab #7 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...