Case Statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Case Statement
# 8  
Old 04-01-2004
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 08:58 AM..
# 9  
Old 04-01-2004
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.
# 10  
Old 04-01-2004
Sorry about that, it wont happen again. Thanks for the help all!
# 11  
Old 04-01-2004
Perderabo, thanks for pointing out the typo - I have corrected it now.

Zeta_Acosta, consider using ksh.
# 12  
Old 04-01-2004
Data

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!
# 13  
Old 04-01-2004
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. Smilie

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.
# 14  
Old 04-01-2004
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 thanksSmilieSmilie Smilie!
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