CS253

CS253: Software Development with C++

Spring 2009

Bash Lab

Using bash

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

For this lab, you should:

  1. Understand how the script works
  2. Copy & paste this into your own file rn, and try it out
  3. Make it actually rename the file, rather than just saying so
  4. Add a usage message if too many or too few arguments are given
  5. Send the usage message to stderr
  6. Complain if a file can’t be renamed

For extra fame & glory, you could:

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

Modified: 2009-01-25T15:46

User: Guest

Check: HTML CSS
Edit History Source
Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2015 Colorado State University
CS Building