The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM



Thread: Case Statement
View Single Post in UNIX Forums - Click on the Thread or Permalink to View Entire Thread -->
  #14 (permalink)  
Old 04-01-2004
Zeta_Acosta Zeta_Acosta is offline
Registered User
 

Join Date: Mar 2004
Posts: 14
Perderabo you are the Don of Unix! Thanks so much for everything! I have found the following solution to my problem though and you might be pleased!

#!/bin/sh //I take it this is why it wont work?

#
# Usage - Display error message and exit
#

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

but when I come to use run it, its say dosRename not found in home directory, is this because its a sh instead of it being a ksh? Or is it something else?

Many many many many thanks !
Reply With Quote