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. Shell Script Page.

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
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-31-2004
Registered User
 

Join Date: Mar 2004
Posts: 14
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Case Statement

Can anyone please tell me why this wont work! Thanks so much!

#!/bin/sh

for file
do
case $file in
*.*.*)
echo Cannot have more than 1 dot
exit
;;

*'**'*)
echo Cannot have more than 1 asterisk
exit
;;

*'*'*|?.)
echo this is a target

;;

*)
echo This is a file
item = file
;;
esac
done

if [! -e "$item"];
echo The File does not exit
exit
fi

It just says Item not found? Thanks
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 03-31-2004
zazzybob's Avatar
Registered Geek
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Two things. You write
Code:
 if [! -e "$item"];
echo The File does not exit
exit
fi
This has two errors and should read
Code:
if [ ! -f "$item" ]; then
   echo "$item does not exist"
   exit
fi
Amend to that and see if that fixes it.

You've also got spaces in your variable assignment, and no $ infront of the file variable name to reference it. You are, in effect, setting the value of item to "file". Another "no, no!"
Do this
Code:
item=$file
as you can see, no spaces either side of the equals.

Elaborate more on the script. What does it do? What is it for? And give the "proper" output from the script.

Cheers
ZB
http://www.zazzybob.com
Reply With Quote
  #3 (permalink)  
Old 03-31-2004
zazzybob's Avatar
Registered Geek
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Also, from your script, it looks like you are using asterisks in file names.

This is a recipe for disaster!!!!
Reply With Quote
  #4 (permalink)  
Old 03-31-2004
Registered User
 

Join Date: Mar 2004
Posts: 14
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Hi Zazzybob, thanks for the help so far! Ok to elaborate more on what I am trying to do:

Well I'm trying to create a script that looks at parameters followed by a target that that specifies either a new main part of the file name or a new extension part but not both.

Rename File1 File2 File3 *.html

So File1 to File3 are params and *.html is is the target.

Then with my loop Im trying to check the validity of them eg less than 2 params, cant have 2 dots, more than one asterisk and doesnt exist.

Then If there are no errors the files are to be renamed in another loop.

But I'm really struggling with it and Its really beginning to get on my nerves as I dont know what Im doing wrong!

Any Suggestions on how I should do it would be fantastic.

By the way, Im doing this becuase I have loads of files that I want to change extensions on and I want to do it this way because I've been trying for ages.

Again many thanks.
Reply With Quote
  #5 (permalink)  
Old 03-31-2004
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,252
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
You will need to use:

Rename File1 File2 File3 '*.html'
otherwise if you had "one.html" and "two.html" in your current directory, they would replace *.html on the command line.

Your error arises from your line:
item = file
This cause your script to try to run a program or script called "item" with two args, "=" and "file".

As zazzybob points out you must lose the spaces.
I assume you mean
item=$file

At that point you will be setting "item" in your loop. When you exit the loop, you then check the last value only to see if it exists.

Many shells support -e, and that may be superior to the -f that zazzybob suggests.

Is there any reason to sh? A more powerful shell like ksh or bash would make this easier.
Reply With Quote
  #6 (permalink)  
Old 04-01-2004
Registered User
 

Join Date: Mar 2004
Posts: 14
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Thanks for that.

Yeah a bash or ksh would probably be more powerful but Im quite new to all this and only really know how to use sh!

Thanks again to you!
Reply With Quote
  #7 (permalink)  
Old 04-01-2004
Registered User
 

Join Date: Mar 2004
Posts: 14
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Angry Case Selection (again)!

Can anyone please help me (again) with this statement:

*'**'*)
echo Cannot have more than 1 asterisk
exit
;;

As you can tell its supposed to drop out if there is more than 1 asterisk, but it doesnt!

Many thanks.

Also can anyone tell me what the structure of these statements are eg I know that * selects the whole string and ? selects a char. But how do I check if there are less than 2 parameters in my statements or if the parameter has to have a dot within the string!

Thank you all so very much (I'm beginning to tear my hair out)!
Reply With Quote
  #8 (permalink)  
Old 04-01-2004
Ygor's Avatar
Moderator
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,208
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
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
  #9 (permalink)  
Old 04-01-2004
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,252
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
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,208
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
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,252
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
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
  #15 (permalink)  
Old 04-01-2004
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,252
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
You probably don't have your home directory in your PATH. (And good for you! I don't either.)

Try using:

./dosRename ....

The leading ./ says to find it in your current directory rather than searching the directories on your PATH.
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes


The 50 most popular UNIX and Linux searches.
Google Search Cloud for The UNIX and Linux Forums
421 service not available, remote server has closed connection ^m automate ftp autosys awk trim bash eval bash for loop boot: cannot open kernel/sparcv9/unix command copy/move folder in unix curses.h cut command in unix daemon process find grep find mtime find null character in a unix file from ip can we get machine name +unix glance unix grep multiple lines grep or grep recursive how to redirect console logs in unix inaddr_any inappropriate ioctl for device lynx javascript mailx attachment mget mtime perl array length ping port remove first character from string in k shell replace space by comma , perl script scp recursive segmentation fault(coredump) sftp script snoop unix stale nfs file handle syn_sent tar exclude tar extract to folder unix unix .profile unix forum unix forums unix internals unix interview questions unix mtime unix simulator unix.com vi substitute while loop within while loop shell script