CSC 271
Homework 3

Assigned 18 Oct; due 3 Nov

For each of the following problems, I'd like to see not only the shell script or alias you came up with, but also one or more tests. To show me your tests, I suggest starting a shell session whose output goes to both the screen and a file (tcsh | tee logfile), typing the various commands you want to demonstrate, then exit and e-mail me the log file.

    1. Write a shell script named backup which takes a list of filenames as arguments and copies each of them to the same name with .backup at the end. For example, if a certain directory contained three files

      this.c
      that.c
      that.h
      
      then backup * would be equivalent to saying
      cp this.c this.c.backup
      cp that.c that.c.backup
      cp that.h that.h.backup
      

    2. A problem with this backup script is that if you backup the same file twice, you may get a warning message because you're overwriting an existing file. You could suppress the warning message, but a better solution is to save a series of backups distinguished by numbers. The first time you type backup foo.c, it copies it to foo.c.1. Then you make some changes to foo.c and type backup foo.c again; the script notices that foo.c.1 is already there, so it copies foo.c to foo.c.2 instead. The third time, you get foo.c.3, and so on. Write a shell script named smart_backup to do this.

    3. Write an alias or shell script named avgsize which takes a directory name as its argument, and computes the average number of bytes in non-directory files in that directory. For example, if the "ls -l" command gave the output
          total 52
          drwxr-xr-x   7 sbloch   system      1024 Nov 21  1996 examples
          drwxr-xr-x   2 sbloch   system       512 Oct 12  1995 fall94
          drwxr-xr-x   3 sbloch   system       512 Sep  4  1996 fall95
          drwxr--r--   3 sbloch   system       512 Aug 26 14:34 fall96
          -rw-r--r--   1 sbloch   system      4865 Oct  7 09:18 hw2.html
          -rw-r--r--   1 sbloch   system      4865 Oct  7 09:18 index.html
          drwxr-xr-x   2 sbloch   system      1024 Sep 12 11:54 syllabus
          -rw-r--r--   2 sbloch   users      11812 Sep 12 11:54 syllabus.dvi
          -rw-r--r--   2 sbloch   users      10418 Sep 12 11:53 syllabus.tex
          drwx------   2 sbloch   system       512 Nov  9  1995 test
          -rw-r--r--   1 sbloch   system     13070 Aug  8  1994 unix.editorial
      
      then the output of avgsize would be 9006 (i.e. (4865+4865+11812+10418+13070)/5). You'll probably need awk; if you put your awk program into a separate file, please turn in that file along with your alias command that invokes it.


    Last modified: Thu Oct 13 16:18:29 EDT 2005
    Stephen Bloch / sbloch@adelphi.edu