COMP2718 Assignment 7

This assignment should be completed modifying this HTML file where indicated and by submitting scripts and output files. The HTML file, scripts, and output files should be uploaded to D2L individually and should use the names mentioned below.

Submit via D2L by 5:00 p.m. on Wednesday, March 23.

Exercise 1: Rewriting Scripts

  1. Rewrite the following script to use the control operators && and || instead of the if statement:
    if [[ $HOME =~ /home ]] ; then
        echo "At Home"
    else
        echo "At Work"
    fi

Submit as ex1_1.sh.

  1. Rewrite the following script to use [[ ]] and (( )). Look up expr and replace it in the script with (( )).
    countf=0 # count files
    for f in *; do
        if test -f $f
        then
            countf=$(expr $countf + 1 )
        fi
    done
    echo $countf

Submit as ex1_2.sh.

  1. Considering that expr is an executable program, how do you think the modification made above would affect the performance of this script.

    Change this with your answer

Exercise 2: BMI Table Generator

Write a bash script that generates a Body Mass Index (BMI) table for weight in pounds from 90 to 210 in steps of 10 and height in inches from 62 to 72. If wt is weight and ht is height, then BMI can be calculated with:

    bmi=$(python -c "print '%6.1f' % (703.0*$wt/($ht**2)) ")

Your script can use printf and echo for formating. The column headings should first be generated using an initial for loop. Following this, the main part of the table can be generated with two nested for loops.

The list of heights can be created with the brace expansion: {62..72}. The list of weights can be created with {9..21}0. The following can be used to initialize two lists of words that the for loops will operate on:

    heights=$(echo {62..72})
    weights=$(echo {9..21}0)

The following is an example of the expected output.

 BMI |   62    63    64    65    66    67    68    69    70    71    72  
-----+-----------------------------------------------------------------
  90 | 16.5  15.9  15.4  15.0  14.5  14.1  13.7  13.3  12.9  12.6  12.2  
 100 | 18.3  17.7  17.2  16.6  16.1  15.7  15.2  14.8  14.3  13.9  13.6  
 110 | 20.1  19.5  18.9  18.3  17.8  17.2  16.7  16.2  15.8  15.3  14.9  
 120 | 21.9  21.3  20.6  20.0  19.4  18.8  18.2  17.7  17.2  16.7  16.3  
 130 | 23.8  23.0  22.3  21.6  21.0  20.4  19.8  19.2  18.7  18.1  17.6  
 140 | 25.6  24.8  24.0  23.3  22.6  21.9  21.3  20.7  20.1  19.5  19.0  
 150 | 27.4  26.6  25.7  25.0  24.2  23.5  22.8  22.1  21.5  20.9  20.3  
 160 | 29.3  28.3  27.5  26.6  25.8  25.1  24.3  23.6  23.0  22.3  21.7  
 170 | 31.1  30.1  29.2  28.3  27.4  26.6  25.8  25.1  24.4  23.7  23.1  
 180 | 32.9  31.9  30.9  30.0  29.0  28.2  27.4  26.6  25.8  25.1  24.4  
 190 | 34.7  33.7  32.6  31.6  30.7  29.8  28.9  28.1  27.3  26.5  25.8  
 200 | 36.6  35.4  34.3  33.3  32.3  31.3  30.4  29.5  28.7  27.9  27.1  
 210 | 38.4  37.2  36.0  34.9  33.9  32.9  31.9  31.0  30.1  29.3  28.5

The script should be called, bmi.sh.

Exercise 3: Regular Expressions

Complete the following Regular Expressions. You should test each one using grep in the following way:

    grep "TEST_STRING" | grep -Ee 'PATTERN'
  1. Provide a regular expression that matches all positive and negative integers.

    Change this with your answer

  2. Provide a regular expression that matches all lowercase strings.

    Change this with your answer

  3. Provide a regular expression that matches all years from 1000 to 9999.

    Change this with your answer

  4. Provide a regular expression that matches all dates of the form YY/MM/DD, where DD is 01 to 31, MM is 01 to 12, and YY is 00 to 99.

    Change this with your answer

Exercise 4: Bash Regular Expression

The following script is used to create a testing directory with a set of image file names. The names end in .PNG for png images, and either .JPG or .jpg for JPEG images.

    mkdir /tmp/tst-images && cd /tmp/tst-images
    touch Img{10..18}.JPG image-{19..23}.jpg i00{3..8}.PNG

Write a bash shell script, called img-rename.sh, that performs the following renaming operations:

You should use regular expression and BASH_REMATCH to perform the renaming. A for loop should also be used.

Execute your script in the tst-images directory. Then do the following to show the result:

    ls -l /tmp/tst-images > ex4.txt

Submit both img-rename.sh and ex4.txt.