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
;;
*)
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?
... 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.
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
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
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)
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)
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)
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)
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)
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)
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)
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)