case in script


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers case in script
# 1  
Old 04-23-2008
case in script

Hello All,
I'll try to get it right this time lol

I am trying to do some validation on my script which renames files.
The ones i am struggling with are:


e.g.

1. myscript myfile myfile2 (so my script changes myfile to myfile2 if myfile exists)
output : myfile does not exist

2. myscript myfile myfile2 blurb
cannot have anything after rename

3. myscript myfile
must have name to rename2

Hope that makes sense. I'll put a bit of my code in to try to make it clearer

case $# in
0) echo "nothing has been entered"
exit
;;
1) echo "file none existent"
exit
;;
2) echo "dosRename: $file: Can't have anything after target"
exit
;;
3) echo "no new name"
exit
;;
*)
# 2  
Old 04-23-2008
Quote:
Originally Posted by jazz8146
1. myscript myfile myfile2 (so my script changes myfile to myfile2 if myfile exists)
output : myfile does not exist
The existence test looks like this:

Code:
test -e "$1" || die "$1 does not exist"

You might as well also check that the file is readable by the user.

Code:
test -r "$1" || die "$1 is not readable"

(The die function is not a standard shell command; implementing that is left as an exercise.)

Quote:
2. myscript myfile myfile2 blurb
cannot have anything after rename
You already check the number of arguments; if it's 3 or larger, that's this case.

Quote:
3. myscript myfile
must have name to rename2
And this is when $# is 1.

Quote:
2) echo "dosRename: $file: Can't have anything after target"
exit
;;
This is not correct; you want exactly two arguments, if I understand your sketch correctly.
# 3  
Old 04-26-2008
Hi,
Thanks for the reply.
I am still a bit puzzled though. The code for the existing file i don't understand how to implement this. Here is the code i have already:

#!/bin/bash

case $# in
0) echo "Usage: Myscript file1 file2"
exit
;;
So here is where i want to code the file existence and other arguments I thought it would go something like this but this doesn't work. Am i on the right lines?
test -e "$1") echo "file dosent exist"
;;
exit;

*) for file
Then my code for the rename goes here


Also the answer to the last question is true i only want exactly two arguements but i want a message if no target is specified and a different message if extra text is entered after the target.
If that makes sense?
# 4  
Old 04-26-2008
Code:
case expression in pattern1) handle pattern1 ;; pattern2) etc;; esac

... compares expression to pattern1, pattern2, etc until a match is found, or falls through.

Your case statement is examining the expression $# and you certainly don't want to compare that to the pattern 'test -e "$1"' (syntax problems notwithstanding). Create a new conditional for that.

Code:
#!/bin/bash

case $# in 2);; *) echo "Usage: $0 file1 file2" >&2; exit 1;; esac

if ! test -e "$1" ; then
  echo "$0: file $1 does not exist" >&2
  exit 2
fi

for file ...

Note that I changed the first case to require exactly two files, based on your syntax message. If $# is 2, the first pattern matches; if not, it falls through to the second pattern, which matches everything else, and causes the script to exit.

Also, take care to set the exit code to signal error conditions; one day, you will be glad you did. (0 means success, everything else is an error. It makes sense to set different exit codes for different error conditions so you can tell which one fired programmatically as well as by examining what the script printed.)

The technique to exit early in case of some error allows you to avoid nesting the conditionals very deep. If you pass all the conditions, you fall through to the main functionality; otherwise, report an error and exit early.

Last edited by era; 04-26-2008 at 03:32 PM.. Reason: Redirect error messages to stderr, too
# 5  
Old 04-26-2008
That's great i understand a lot better now Smilie
My usage is a lot better now i have done what you said but it does not like end of the if

syntax error near unexpected token `fi'
# 6  
Old 04-26-2008
Works here, are you sure you copy+pasted correctly?
# 7  
Old 04-28-2008
Yeah i got it working eventually. However when i try to use my script now it is telling me incorrect use.
So i type in
myscript file.* test.*
and i get "usage file1 file2" as if i am using it incorrectly. Is this becasue my script is for file names AND extensions?

If i wanted to add another statement would i add to this to the case i already have?

i.e.

case $# in 2);; *) echo "Usage: $0 file1 file2" >&2; exit 1;;
'..') echo "cannot have more than one dot" exit ;;
another case) echo "blah blah blah" exit ;; esac


if ! test -e "$1" ; then
echo "$0: file $1 does not exist" >&2
exit 2
fi

would this be correct?

Last edited by jazz8146; 04-28-2008 at 05:48 AM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Change first letter of a word from lower case to upper case

Hi all, I am trying to find a way to change first letter in a word from lower case to upper case. It should be done for each first word in text or in paragraph, and also for each word after punctuation like . ; : ! ?I found the following command sed -i 's/\s*./\U&\E/g' $@ filenamebut... (7 Replies)
Discussion started by: georgi58
7 Replies

2. Shell Programming and Scripting

Conversion from Upper Case to Lower Case Condition based

Hello Unix Gurus : It would be really appreciative if can find a solution for this . I have records in a file . I need to Capitalize the records based on condition . For Example i tried the following Command COMMAND --> fgrep "2000YUYU" /export/home/oracle/TST/data.dat | tr '' ''... (12 Replies)
Discussion started by: tsbiju
12 Replies

3. Shell Programming and Scripting

[Solved] Change Upper case to Lower case in C shell

Is there a command that can switch a character variable from UPPER case to lower case? like foreach AC ( ABC BCD PLL QIO) set ac `COMMAND($AC)` ... end Thanks a lot! (3 Replies)
Discussion started by: rockytodd
3 Replies

4. Shell Programming and Scripting

sed ignoring case for search but respecting case for subtitute

Hi I want to make string substitution ignoring case for search but respecting case for subtitute. Ex changing all occurences of "original" in a file to "substitute": original becomes substitute Origninal becomes Substitute ORIGINAL becomes SUBSTITUTE I know this a little special but it's not... (1 Reply)
Discussion started by: kmchen
1 Replies

5. Shell Programming and Scripting

data array needs to change upper case to lower case

Hi all, i have a data array as followes. ARRAY=DFSG345GGG ARRAY=234FDFG090 ARRAY=VDFVGBGHH so on.......... i need all english letters to be change to lower case. So i am expecting to see ARRAY=dfsg345ggg ARRAY=234fdfg090 ARRAY=vdfvgbghh so on........ If i have to copy this data in... (8 Replies)
Discussion started by: usustarr
8 Replies

6. Shell Programming and Scripting

Script to Convert Upper case to Lower case

Hi All I have a script which extracts values from a Database (A persons name) and puts it into a variable in my script IE: $NAME However the Value in the DB is all in uppercase and contains the users first name and last name EG: > echo $NAME GRAHAM BOYLE > What I need is only the... (7 Replies)
Discussion started by: grahambo2005
7 Replies

7. Shell Programming and Scripting

convert upper case to lower case in ascript

I have a package to install and the installation script which does it . The files/directories names in the script are all lower case but the actual package has everything in upper case - file names, directories . I don't want to rename directories and files in the package - it has a lot of them . ... (2 Replies)
Discussion started by: vz6zz8
2 Replies

8. Shell Programming and Scripting

Script needed to select and delete lower case and mixed case records

HELLO ALL, URGENTLY NEEDED A SCRIPT TO SELECT AND DELETE LOWER AND MIXED CASE RECORDS FROM A COLUMN IN A TABLE. FOR EXAMPLE : Table name is EMPLOYEE and the column name is CITY and the CITY column records will be: Newyork washington ... (1 Reply)
Discussion started by: abhilash mn
1 Replies

9. UNIX for Dummies Questions & Answers

lower case to upper case string conversion in shell script

How can convert a Lower case variable value to an upper case in the kron shell script. (3 Replies)
Discussion started by: dchalavadi
3 Replies
Login or Register to Ask a Question