CS253: Software Development with C++

Fall 2020

Bash

Bash Lab

Basic bash programming

Script

Here is a small bash script. It changes file suffixes:                 

    #! /bin/bash

    old_suffix=$1
    new_suffix=$2

    for f in *.$old_suffix
    do
        new_name=${f%.*}.$new_suffix
        echo Rename $f to $new_name
    done

You might use it like this, to rename all of your C files (ending in .c) to be C++ files (ending in .cpp):                 

    ./rn c cpp

Copy the script, above, to the file rn. No suffix—just the two-character filename rn with nothing after. You will modify rn, and turn it in for credit.                 

For this lab, you should:                 

  1. Understand how the script works
  2. Understand why we said ./rn as opposed to just rn
  3. Copy & paste this into your own file rn, and try it out
  4. Make it actually rename the file, rather than just saying so
  5. Add a usage message if too many or too few arguments are given
  6. Send the usage message to standard error
  7. Complain (to standard error) if a file can’t be renamed
  8. Not clobber existing files

For extra fame & glory, you could:                 

  1. Add a -n option that doesn’t rename, but just says what it would do
  2. Add a -v option that renames verbosely
  3. Complain if no files have the given suffix
  4. Make it work for filenames and suffixes that contain spaces
  5. Make it work for suffixes that are wildcards (whatever that means)

How to submit your work:

In Canvas, check in the file rn to the assignment “Lab02”.                 

How to receive negative points:

Turn in someone else’s work.