Week 04 Tutorial Questions

Objectives

  1. A COMP2041 student wrote this script, named start_lab04.sh, to run before the Week 4 lab.

    cat start_lab04.sh
    #! /bin/dash
    
    cd ~/labs/04
    
    ex1=jpg2png
    ex2=email_image
    ex3=date_image
    ex4=tag_music
    

    But when he ran his script, it didn't seem to work:

    pwd
    /home/z1234567
    ./start_lab04.sh
    pwd
    /home/z1234567
    echo $ex1 $ex2 $ex3 $ex4
    
    

    Why not? How can the script be fixed?

  2. Write a shell script, is_business_hours which exits with a status of 0 if the current time is between 9am - 5pm, and otherwise exits with a status of 1.

  3. COMP2041 student Shruti has a 'friends' subdirectory in her home directory that contains images of her many friends.
    Shruti likes to view these images often and would like to have them appear in other directories within her CSE account so she has written a shell script to symlink them to the current directory:

    for image_file in $(ls ~/friends); do
        ln -s "~/friends/$image_file" .
    done
    

    The links created by Shruti's script are broken.

    Why? How can she fix her script?

  4. The course code for COMP2041 has been changed to COMP2042 and the course code for COMP9044 has been changed to COMP9042.

    Write a shell script, update_course_code.sh which appropriately changes the course code in all the files it is given as arguments.

  5. Modify update_course_code.sh so if given a directory as an argument it updates the course codes in files found in that directory and its sub-directories.

  6. CSE systems have a command, mlalias, which prints information about a specified mail alias.
    For example:

    mlalias cs2041.24T1.tutors
              Alias: cs2041.24T1.tutors
        Description: 24T1 COMP2041 tutors
              Flags: personal, public, unprivileged, members_can_post, closed
          Addresses:
                     z5312070
                     z5358496
                     z5345805
                     z5416305
                     z5210932
                     z5358698
                     z5316004
                     z5425577
                     z5208931
                     z5416950
                     z5311762
                     z5419252
                     z5437869
                     z5422337
                     z5115658
                     z5360130
                     z5418202
                     z5421214
                     z5421623
                     z5310685
                     z5289637
                     z5309949
                     z5366832
                     z5359356
                     z3121449
                     z5360319
                     z5361210
                     z5258575
                     z5419773
                     z5267282
                     z5363122
                     z5359557
                     z5420849
                     z5160350
                     z5258927
                     andrewt
             Owners: cs2041
            Senders: @COMP2041_Lecturer, @COMP2041_Supervisor, @Employee
    

    Convert the output of the mlalias command into a new line separated list of UNSW zIDs,
    like this:

    z5312070
    z5358496
    z5345805
    z5416305
    z5210932
    z5358698
    z5316004
    z5425577
    z5208931
    z5416950
    z5311762
    z5419252
    z5437869
    z5422337
    z5115658
    z5360130
    z5418202
    z5421214
    z5421623
    z5310685
    z5289637
    z5309949
    z5366832
    z5359356
    z3121449
    z5360319
    z5361210
    z5258575
    z5419773
    z5267282
    z5363122
    z5359557
    z5420849
    z5160350
    z5258927
    
  7. CSE system have a command, acc, which prints information about a specified user.
    For example:

    acc z5417087
               User Name : z5417087            Aliases : xavc
                     Uid : 3731                    Gid : 3731
                 Expires : 06oct2024
    
                  Groups : cs1521[10jun2024], classadmin[03mar2025]
                         : tsexternal[25apr2024], ts[25apr2024]
           Group classes : classadmin[03mar2025], ts[25apr2024]
                         : tsexternal[25apr2024]
            User classes : 3778_Student[10mar2024], 3779_Student
                         : COMP1521_Lecturer[10jun2024]
                         : COMP3131t1_Student[10jun2024]
                         : COMP4141t1_Student[10jun2024]
                         : SummerResearch_Student[26apr2024]
            Misc classes :
    
                    Name : Mr Cooney, Xavier (Xavier Cooney)
                Position : Casual Academic (Sch: Computer Science & Eng)
             UNSW Number : 5417087
               UNSW Mail : x.cooney@unsw.edu.au
               UNSW Home : //INFPWFS219.ad.unsw.edu.au/Student037$/z5417087
                CSE Home : /import/reed/5/z5417087
    

    Write a pipeline which converts the output of acc into a new line separated list of courses the person is enrolled as a student in,
    like this:

    COMP3131
    COMP4141
    

    Make sure you don't include COMP1521.

  8. Use the pipeines from the above 2 questions to write shell commands which print a list of courses taken by COMP2041 students with counts of how many COMP2041 students take each,
    like this:

        7 COMP6771
        4 COMP3511
        3 COMP4952
        3 COMP4951
        3 COMP3141
        2 COMP9417
    ...
    
  9. Write a shell script named is_prime.sh which given an integer as an argument, tests whether it is prime and prints a suitable message:

    is_prime.sh 42
    42 is not prime
    is_prime.sh 113
    113 is prime
    

    Your script should exit with a non-zero exit status if its argument is not prime.

    Write a second script named primes.sh which uses the first script to print all primes less than a specified value, e.g:

    primes.sh 100
    2
    3
    5
    7
    11
    13
    17
    ...
    79
    83
    89
    97
    
  10. We are working on a C program in an editor, and we'd like to run a script in another window which recompiles the program every time we save the file in the editor.

    Write a shell script named recompile.sh which given a C file as argument, recompiles the program if the file changes and if the compile succeeds runs the program.

    Hint: you can use the program stat to print the time when the files was last modified (as seconds since 1970-01-01) , like this:

    $ stat -c '%Y' main.c
    1615346166
    

    Repeatedly checking if a file has changed is not ideal. It consumes CPU/power.

    The program inotifywait is available on many Linux system linux (not CSE).

    inotifywait -e main.c will exit when main.c is modified

    inotifywait is efficient because waits for the operating system interface to notify it the file has changed..

    Modify your script to use inotifywait

  11. The shell variable $PATH contains a colon-separated list of directories. which will be searched when executing a command.

    Write a shell script named which.sh which given a program name as argument, uses to ls to print details of matching files in directories in $PATH

    For example:

    ./which.sh cat
    -rwxr-xr-x 1 root root 43936 Sep 24 18:36 /bin/cat
    ./which.sh clang
    llrwxrwxrwx 1 root root 24 Jan 12 01:49 /usr/bin/clang -> ../lib/llvm-11/bin/clang
    ./which.sh lost
    lost not found
    

    The shell builtin which does something similar:

    which cat
    /bin/cat
    which clang
    /usr/bin/clang
    which lost
    
    

    but don't try using which. Use the usual programs we've been using to write scripts such as tr and test.

    Think about if any characters in directory names migh break your answer, e.g spaces.