Home > Undergraduate > Open Source Tools and Scripting >    Labs

  CITS4407/CITS2003 OPEN SOURCE TOOLS AND SCRIPTING
 
 

Lab 6: Arithmetic and Find

Questions

  1. Write a script which prints a triangle of * characters. The first row should be a single *, the second **, and so on. You can use echo -n to print output without a newline at the end. The number of rows is specified by the command line argument -r. If no argument is provided, print 4 rows.
    > ./triangle.sh -r 1
    *
    
    > ./triangle.sh
    *
    **
    ***
    ****
    
    > ./triangle.sh -r 6
    *
    **
    ***
    ****
    *****
    ******
    
    #!/bin/bash
    
    if [[ $# -gt 1 && $1 -eq '-r' ]]
    then
        size=$2
    else
        size=4
    fi
    
    row=1
    while [[ $row -le $size ]]
    do
        col=1
        while [[ $col -le $row ]]
        do
            echo -n '*'
            col=$((col+1))
        done
        echo
        row=$((row+1))
    done
    
  2. Modify your triangle script so that instead of printing *s, it prints a character selected using the rules below:
    • If you are printing the last column, print \
    • Otherwise, if you are printing the first column, print |
    • Otherwise, if the row number is odd, print L
    • Otherwise, print *
    > ./triangle.sh -r 1
    \
    
    > ./triangle.sh
    \
    |\
    |L\
    |**\
    
    > ./triangle.sh -r 6
    \
    |\
    |L\
    |**\
    |LLL\
    |****\
    
    #!/bin/bash
    
    if [[ $# -gt 1 && $1 -eq '-r' ]]
    then
        size=$2
    else
        size=4
    fi
    
    row=1
    while [[ $row -le $size ]]
    do
        col=1
        while [[ $col -le $row ]]
        do
            if [[ $col -eq $row ]]
            then
                echo -n '\'
            elif [[ $col -eq 1 ]]
            then
                echo -n '|'
            elif [[ $((row%2)) -eq 0 ]]
            then
                echo -n 'L'
            else
                echo -n '*'
            fi
    
            col=$((col+1))
        done
        echo
        row=$((row+1))
    done
    
  3. Find the file called kangaroo.cow. Hint: start in /usr
  4. find /usr -name kangaroo.cow
  5. Fine the directory called mess under /lab
  6. find /lab -name mess -type d
  7. Scattered across the subdirectories of mess are 5 scripts with the word lab in their name. Write a find command to make these scripts executable and move them to /lab/week6/safe. Remember that wildcards in find patterns must be quoted, otherwise shell will expand them.
  8. find /lab/week6/mess -type f -name '*'lab'*' -exec chmod +x {} \; -exec cp {} /lab/week6/safe \;

Bonus

  1. Modify triangle.sh to support command-line arguments for additional functionality:
    --bottom -b Print a line of dashes (-) beneath the triangle to form a bottom edge
    
    --rows, -r=ROWS Number of rows to print (default 4)
    
    --character, -c=CHAR Use CHAR in place of * when printing. You may assume CHAR is a single-character string
    
    --help, -h Display this help message and exit
    


Department of Computer Science & Software Engineering
The University of Western Australia
Last modified: 8 February 2022
Modified By: Daniel Smith

UWA