#!/bin/bash # vi:set ts=8 sw=4 et sta: # # Author : Clark J. Wang # License: GPLv2 # # $Date$ # $Author$ # $HeadURL$ # $Revision$ # #--------------------------------------------------------------------# { declare g_progName=$0 declare g_sedExp declare g_all declare ESC=$'\033' } #--------------------------------------------------------------------------# usage() { cat << END Usage: $g_progName [-a] [-h] sedExp file ... -a Files and directories. Without this option, only files. -h Help sedExp Sed command Report bugs to Clark J. Wang END exit $1 } #--------------------------------------------------------------------------# getargs() { declare opt OPTIND=1 OPTERR=0 while getopts ":ah" opt "$@"; do case $opt in a) g_all=yes ;; h|:|'?') [ $opt = h ] && usage 0 || usage 1 ;; esac done shift $((OPTIND - 1)) if [ $# -lt 2 ]; then usage 1 fi g_sedExp=$1 shift if ! sed -e "$g_sedExp" < /dev/null 2> /dev/null; then echo "Invalid sed command: \`$g_sedExp'" exit 1 fi g_files=( "$@" ) } #--------------------------------------------------------------------------# do_rename() { declare file newFile for file in "${g_files[@]}"; do printf '%-20s -> ' "$file" if [ ! -e "$file" ]; then echo "[$ESC[0;31mNot found$ESC[0m]" continue fi if [ -d "$file" -a "$g_all" != yes ]; then echo "[$ESC[0;31mDir skipped$ESC[0m]" continue fi newFile=$(echo "$file" | sed -e "$g_sedExp") if [ "$file" = "$newFile" ]; then echo "[$ESC[0;31mNot changed$ESC[0m]" elif [ -e "$newFile" ]; then echo "[$ESC[0;31mDst exists$ESC[0m]" else echo "$newFile" mv "$file" "$newFile" fi done } #-- main() ----------------------------------------------------------------# { getargs "$@" do_rename }