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
  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)

User: Guest

Check: HTML CSS
Edit History Source

Modified: 2018-01-25T08:58

Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2018 Colorado State University
CS Building