Final Time now!
Why is this just being echoed to the screen, what have I done now! Please Please Please help beginning to hate shell!
#!/bin/sh
Usage() {
[ $# -ne 0 ] && echo "\n$*\n" >&2
cat <<-EOD_USAGE >&2
Usage: $0 file... target
file... List of files to rename
target Target name
EOD_USAGE
exit 1
}
#
# Get and verify arguments
#
# Argument count
[ $# -lt 2 ] && Usage "Missing arguments."
# Files to rename
while [ $# -gt 1 ]
do
file=$1
case "$file" in
*.*.*|*\**) Usage "Invalid file name : $file" ;;
esac
[ -e "$file" ] || Usage "File not found : $file"
file_list="$file_list $file"
shift
done
# Target name
target=$1
case "$target" in
*.*.*|*\**\**) Usage "Invalid target name : $file" ;;
esac
target_nam=`echo "$target" | cut -d. -f1`
target_ext=`echo "$target" | cut -d. -f2`
#
# Rename loop
#
for file in $file_list
do
# Actual file name and extension
file_nam=`echo "$file" | cut -d. -f1`
file_ext=`echo "$file" | cut -d. -f2`
# New file name and extension
if [ "$target_nam" = '*' ]
then new_nam="$file_nam"
else new_nam="$target_nam"
fi
if [ "$target_ext" = '*' ]
then new_ext="$file_ext"
else new_ext="$target_ext"
fi
[ -n "$file_ext" ] && file_ext=".$file_ext"
[ -n "$new_ext" ] && new_ext=".$new_ext"
new="${new_nam}${new_ext}"
# Rename
echo "Rename file $file to $new ..."
if [ -e "$new" ]
then echo "Not renamed, target file already exists : $new" >&2
else mv "$file" "$new"
fi
done
Thanks so MUCH!