Case Statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Case Statement
# 1  
Old 03-31-2004
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  
Old 03-31-2004
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  
Old 03-31-2004
Also, from your script, it looks like you are using asterisks in file names.

This is a recipe for disaster!!!!
# 4  
Old 03-31-2004
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  
Old 03-31-2004
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  
Old 04-01-2004
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  
Old 04-01-2004
Network 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)!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Case statement help

Hi I am new to shell scripting, I wanted to make a shell script that has a case statement asking the user to select their city 1)london 2)tokyo 3) etc., I then want the users input to be stored in a variable and echoed out in another script; so for example if the user selects tokyo, tokyo city code... (2 Replies)
Discussion started by: scriptnewbie
2 Replies

2. Homework & Coursework Questions

Case Statement

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Hey, guys I really need some help with a project. "Write a shell program that examines the command line... (8 Replies)
Discussion started by: sk192010`
8 Replies

3. Shell Programming and Scripting

Case Statement

Hey, guys I really need some help with a project. "Write a shell program that examines the command line arguments, counts and collects the number of options. Basically it has to collect and count the arguments that start with a "-" and the one's that don't start with a - I know I have to use... (2 Replies)
Discussion started by: sk192010`
2 Replies

4. Shell Programming and Scripting

Case statement

Hello, The standard case statement :- case "$1" in "IE0263") commands;; "IE0264") commands;; esac is it possible to have :- case "$1" in "IE0263" OR "IE0878") commands;; "IE0264") commands;; esac Thanks (4 Replies)
Discussion started by: jmahal
4 Replies

5. Shell Programming and Scripting

case statement

Hi, I am writing case statement to execute some finction, my requirement is once one of the case statement is executed again it has to prompt for the option. for script in `echo "$Script_Selected"` do case $script in 1) getNoOFActUsers ;; 2) moveServerrOORotation ;; ... (2 Replies)
Discussion started by: Satyak
2 Replies

6. Shell Programming and Scripting

help with case statement

I am writing a script to pull diskspace information from our servers. Here is the script that I wrote: #!/bin/ksh for host in `cat /oper/hosts/esc.misc` do ssh -q -o ConnectTimeout=10 operator@$host df -h|grep "/dev/" |egrep '8%|9%|100%' | awk '{print H " " "at " $5 " with " $4 "... (1 Reply)
Discussion started by: rkruck
1 Replies

7. Shell Programming and Scripting

case statement

Hi all, I think i'm asking a sqtupid question here.. i'm using case sttament, what is the syntax or symbol for "or"? I thought was || here a quick sample of my case statment echo "Would you like to update your detail ?" read response case $response in ... (2 Replies)
Discussion started by: c00kie88
2 Replies

8. UNIX for Dummies Questions & Answers

If or Case Statement

I want to write a program with the following variables: a=7000 b=24000 c=613.8 The user can enter two words: Vivid or Blue for example. The challenge is that the user might not want to write the words the way they appear. The user can write V or v or vivid or Vivid or write Blue or blue, or B,... (1 Reply)
Discussion started by: Ernst
1 Replies

9. Shell Programming and Scripting

case statement

hi all i'm writing a script and in it i need to prompt the user if the entered value is correct or not ,i wrote the following and its not working ,its executing the script even if i enter Y/N pls any help is appreciated echo "\nAre you sure you entered the right Destination Environment? y :... (5 Replies)
Discussion started by: bkan77
5 Replies

10. Shell Programming and Scripting

case statement

Hi all, is it possible to create a 'dynamic' case statement. ie select option in `ls` do case satement depending on results of the above `ls` done I hope I have explained this ok! Thanks Helen (1 Reply)
Discussion started by: Bab00shka
1 Replies
Login or Register to Ask a Question