CS 2204: Homework #4

What to turn in: A legible paper copy giving your answers

  1. (3 points) We know that when we run cat with multiple arguments, cat prints the contents of the first file (argument), followed by the contents of the second, and so on. Let us write our own bash script (call it mycat) that does the same thing and, in addition, prefaces each file output with a line indicating the filename. So, suppose we have two files local.accounts and national.accounts; when we run mycat local.accounts national.accounts, we get the following six-line output:

    ==> Below lines are taken from local.accounts <==
    Adam 1234567 $67.5
    Michael 4219485 $59.5
    ==> Below lines are taken from national.accounts <==
    Kathleen 89 $1000.00
    James 020 -$35.05

    where local.accounts contains the two lines about Adam and Michael, and national.accounts contains the lines about Kathleen and James. Your mycat program must work for any arguments typed in, not just the example given here. You may assume that mycat will be invoked with at least one argument.
  2. (4 points) Write a bash script called ren to take a set of files as arguments and renames any files that have spaces in them by replacing the space with a dot . . Any file that does not contain spaces should be unchanged. For instance,

    ren Hello\ World book

    will rename Hello World to Hello.World and leave the file book unchanged (note that we have to escape the space, else the shell will think you are passing three arguments). You may assume that ren will be called with at least one argument.
  3. (3 points) Write a bash script called mycp that takes two filenames as arguments and copies one over to the other. However, the semantics of mycp are different from the UNIX command cp. Typically, cp a b means that file a is to be copied over to b, over-writing what was already in b. Whereas mycp a b means that file b is to be copied over and over-write file a. Essentially, the placeholders for giving the source and destination files are reversed. You may assume that mycp will be called with exactly two arguments, both of which are filenames.