The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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 10:04 PM
case statement bkan77 Shell Programming and Scripting 5 09-11-2007 05:54 PM
Case statement problem gzs553 UNIX for Advanced & Expert Users 6 11-14-2006 04:24 PM
turning case into a if statement brentdeback Shell Programming and Scripting 2 12-03-2005 12:12 AM
case statement Bab00shka Shell Programming and Scripting 1 07-15-2002 05:31 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-31-2004
Zeta_Acosta Zeta_Acosta is offline
Registered User
  
 

Join Date: Mar 2004
Posts: 14
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
  #2 (permalink)  
Old 03-31-2004
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
  
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
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
  #3 (permalink)  
Old 03-31-2004
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
  
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
Also, from your script, it looks like you are using asterisks in file names.

This is a recipe for disaster!!!!
  #4 (permalink)  
Old 03-31-2004
Zeta_Acosta Zeta_Acosta is offline
Registered User
  
 

Join Date: Mar 2004
Posts: 14
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.
  #5 (permalink)  
Old 03-31-2004
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
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.
  #6 (permalink)  
Old 04-01-2004
Zeta_Acosta Zeta_Acosta is offline
Registered User
  
 

Join Date: Mar 2004
Posts: 14
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!
  #7 (permalink)  
Old 04-01-2004
Zeta_Acosta Zeta_Acosta is offline
Registered User
  
 

Join Date: Mar 2004
Posts: 14
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)!
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:21 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0