The UNIX and Linux Forums  

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


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
sh -help with case statement (should be simple) kuliksco Shell Programming and Scripting 1 11-19-2007 06:04 PM
case statement bkan77 Shell Programming and Scripting 5 09-11-2007 02:54 PM
Case statement problem gzs553 UNIX for Advanced & Expert Users 6 11-14-2006 12:24 PM
turning case into a if statement brentdeback Shell Programming and Scripting 2 12-02-2005 08:12 PM
case statement Bab00shka Shell Programming and Scripting 1 07-15-2002 02:31 AM

Reply
 
LinkBack Thread Tools Display Modes
  #8 (permalink)  
Old 04-01-2004
Ygor's Avatar
Moderator
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,221
It does seem to work:

$ set a'**'b
$ case $1 in *'**'*) echo true ;; esac
true

P.S. Why go to the bother of writing a rename script? Just do...

for file in files*.txt
do
mv -i $file ${file%.*}.html
done

Last edited by Ygor; 04-01-2004 at 05:58 AM.
Reply With Quote
Forum Sponsor
  #9 (permalink)  
Old 04-01-2004
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,314
Zeta_Acosta, please don't start a 2nd thread like that. I have merged your threads.

I believe that Ygor has a typo. That mv command probably should be:

mv -i $file ${file%.*}.html

However, by starting a new thread, you concealed from Ygor that you're determined to use an antique shell. That won't work with the old Bourne shell.
Reply With Quote
  #10 (permalink)  
Old 04-01-2004
Registered User
 

Join Date: Mar 2004
Posts: 14
Sorry about that, it wont happen again. Thanks for the help all!
Reply With Quote
  #11 (permalink)  
Old 04-01-2004
Ygor's Avatar
Moderator
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,221
Perderabo, thanks for pointing out the typo - I have corrected it now.

Zeta_Acosta, consider using ksh.
Reply With Quote
  #12 (permalink)  
Old 04-01-2004
Registered User
 

Join Date: Mar 2004
Posts: 14
Unhappy

Ok I know that Im beginning to get on peoples nerves and I apologise for that! But can I ask one more question in relation to this script (please dont shout about it being a sh, Im sorry)

Why is this not working correctly now:

input: dosRename Mike *.mike

#!/bin/sh

for file
do
case $file in
*'*'*|?.)
if [ $file = *.[a-z] ]
then type=extension
elseif [$file = [a-z].*]
type=main
fi
echo $type
;;

*)
echo $file is a File
;;
esac
done

output:
$ dosRename Mike *.mike
Mike is a File
BLANKLINE:

Thank you all so much for the patience!
Reply With Quote
  #13 (permalink)  
Old 04-01-2004
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,314
I don't think that we're shouting. But you are continually tripping over the shortcomings of sh and then inquiring about it. We don't meant to get on your nerves.

Whether or not something like:
if [ $file = *.[a-z] ]
will even work at all depends on the contents of the current directory. I had to copy the script to an otherwise empty directory to ensure that the shell would not match "*.[a-z]". Once I did that that, the test command will see the string *.[a-z]. But the test command does not do pattern matching. So to trigger the "if" statement, copy your script to an otherwise empty directory and then use:
dosRename Mike '*.[a-z]'

You see that is the only string that will match the *.[a-z] in the if statement.

I guess that I'm not supposed to mention that this would be a snap with ksh, so I won't.

With sh, the only way to match a string against a pattern is with the case statement. Or you can do stuff like using sed to delete the pattern and see if anything is left in the string. The case statement idea is the better solution since it is a builtin command.
Reply With Quote
  #14 (permalink)  
Old 04-01-2004
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
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 02:49 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0