Home > Undergraduate > CITS4407 Open Source Tools and Scripting >    Labs

  CITS4407 OPEN SOURCE TOOLS AND SCRIPTING
 
 

Lab 5: glob, loops, shift

Questions

  1. Your friend has sent you more game data in updated_cool_online_game.csv. Which players have improved their scores?
  2. The following questions make use of globbing
    1. List all files in /bin that start with the letter g
    2. List all files in /bin that end with the letter t
    3. List all files in /bin that start with the letter g and end with the letter t, where the second letter is not e
  3. Write a script texter.sh that accepts any number of command line arguments. It should create a text file using the first argument as the file name and the second argument as the file content. Repeat for all remaining arguments. If there is an odd number of arguments, leave the last file blank.
    > ./texter.sh f1 "this is file 1 content" f2 "deadbeef"
    > ls
    
    f1 f2
    
    > cat f1
    
    this is file 1 content
    
    > cat f2
    
    deadbeef
    
  4. Write a script sizes.sh which prints stats about each file in the current directory. For each file it should print the filename and file size After printing data for all files, print the size and name of the largest file, then print the sum of all the file sizes. Your script should ignore directories. Hint: You can obtain the size of a file as a number by using wc -c with cut
    > ./sizes.sh
    160786 Alice_in_Wonderland.txt
    448821 Frankenstein.txt
    2525 cool_online_game.csv
    363 sizes.sh
    4320 lots_of_the.txt
    2549 updated_cool_online_game.csv
    Largest is 448821 in Frankenstein.txt
    Sum is 619364
    
    Note your final size may vary if you've changed the size of any files (such as the sizes.sh script)
  5. Modify sizes.sh to accept the command line options as specified below. Use shift and case to achieve this:
    -d DIRECTORY Count files in DIRECTORY instead of .
    
    -h Display this help message and exit
    
  6. When would you prefer a while loop over a for loop? When would you prefer a for loop? What is a danger of using a while loop?

Bonus

Modify sizes.sh to accept the following command line option:
-r Instead of ignoring directories, recursively run stats.sh in each directory found.


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

UWA