COMP2021 Lab4: Shell Programming


Remember: You have to change the file permissions to run shell scripts!

  1. File Processing: Write a shell script "file_lines.sh" to find all the txt files in a directory and print the number of lines in each file.
    Example:
    a.txt:
    Hello World!
    
    b.txt:
    Hello World!
    It is a great day!
    
    
    $ file_lines.sh document
    a.txt has 1 line(s).
    b.txt has 2 line(s).
    
  2. String Manipulation: Create a shell script "palindrom.sh" to judge whether a string is Palindrome. This string is sent to the script as command line argument.
    Example:
    $ palindrom.sh abcba
    String is palindrome
        
    $ palindrom.sh acccd
    String is not palindrome
    
  3. Friday the 13th: Creat a shell script called "friday13.sh", which lists all the occurrences of Friday the 13th in a user-input year range. When the shell program is executed, it should get 2 input parameters which indicate the year range of inquiry inclusively, the program needed to list all the months and years which happened to be Friday the 13th, starting from latest date to the earliest date. You may assume the input year range is valid (the first one is smaller than the second one).
    Example:
    $ friday13.sh 2005 2011
    May-2011
    Aug-2010
    Nov-2009
    Mar-2009
    Feb-2009
    Jun-2008
    Jul-2007
    Apr-2007
    Oct-2006
    Jan-2006
    May-2005
    
  4. Write a shell script "gcd.sh" to find the Greatest Common Divisor(GCD) for 2 given numbers. Those two numbers are sent to the script as command line argument.
    Example:
    $ gcd.sh 54 24
    gcd of 54 and 24 is 6
    
  5. Batch Processing: There are N input files and each file contains two numbers. Note that here N is not a fixed number, and these N files are named as file1, file2, ...,fileN. write a shell script "batch.sh" to call "gcd.sh" to compute the Greatest Common Divisor(GCD) for all these N input files. N should be sent to the script as command line argument.
    Example:
    file1:
    6 4
    file2:
    54 24
    
    $ batch.sh 2
    gcd of 6 and 4 is 2
    gcd of 54 and 24 is 6
    

Lab Answers

Please find the answers for lab 4: file_lines.sh, palindrom.sh, Friday13.sh, gcd.sh and batch.sh.

Any inquiry please contact your TA by email