CS253

CS253: Software Development with C++

Spring 2018

Bash

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. 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 stderr
  7. 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: 2017-01-19T13:29

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