Validating uppercase/lowercase of user input with perl compared to unix folders


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Validating uppercase/lowercase of user input with perl compared to unix folders
# 1  
Old 01-09-2012
Validating uppercase/lowercase of user input with perl compared to unix folders

Hi,

I need to copy files from a source directory to a destination directory in unix.
I'm using the file::copy for the actual copy.

The problem is that the source and dest directories are supplied by different users, who might type the name of the directories in various combinations of lower case and upper case. This would be easy to solve, except no one kept here a naming standard, so the actual directories in unix can also be different combinations of uc and lc.

I was told that in most paths, only the two last directories would be in random case (that is: /Bla/Bla/Bla/random case/random case). I got around that by splitting the paths, taking the last two vars, then going over the directories with opendir and comparing everything under the directory to a non-case regex of the var I need (Smilie):
Code:
$slash = "/";
@split_path = split($slash,$dstpath);
$last = $split_path[$#arr];
$before_last = $split_path[$#arr-1];
pop @split_path;
pop @split_path;
$path = join($slash,@split_path);
opendir (DIR,$path);
while ($dir_name = readdir(DIR)) {
     $path = $path.$slash.$dir_name if ($dir_name =~ /^$before_last$/i);
}
closedir (DIR);
opendir (DIR,$path);
while ($dir_name = readdir(DIR)) {
     $path = $path.$slash.$dir_name if ($dir_name =~ /^$last$/i);
}

And this works... but what do I do in the case the folders before those two are also random case? Is there a simple recursive way to tackle this?

Also would this check:
Code:
&& (-d $dir_name)

work to make sure I found a directory and not a file?

Many thanks Smilie
# 2  
Old 01-09-2012
I don't think this can be rigorously solved. UNIX will allow them to create many folders with the same name in different casing. How would users differentiate between them now?

How I'd handle it would be going through their folders and renaming them all lower-case for them.

If you're stuck with this mess, loop through whole folders converting everything to lowercase before comparing. If they match as lowercase, it's "correct", so use the unconverted mixed-case foldername.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 01-09-2012
Hi Furou,

Strange issue if I understood correctly. Names in Unix (most of them at least) are case sensitive, so users should provide the correct name and not leave you the job of searching for the correct one.

What about if the user gives you 'myFolder' and there are two of them, one 'myfolder' and other MYFOLDER?

I would use the grep() function instead open each dir and read all its contents.

Regards,
Birei
This User Gave Thanks to birei For This Post:
# 4  
Old 01-09-2012
The paths end with /enviornment/application_code.

So it could be /..../Prod/XX, /PROD/xx, /prod/Xx, whatever whoever created the folders felt like naming them. Also beneath them are Main or main or MAIN, and beneath that too - Scr, SCR, scr....

Then I might be supplied those same folder names as parameters in different combos.. and even if not, even if I always get the parameters in the same case, I still need to find out in what case exactly that folder is written in, because I need to transfer files there. Otherwise I would've used a lower case comparison like you suggested Corona688.

birei, so I can I use the code I wrote (I mean the same logic), but replace the whole opendir->while loop, with a grep?

Worst case scenario I'll give the users the responsibility (since they are in charge of whatever's in those directories) to find out how the folders are written and make sure the names they supply the script and the actual names correspond, themselves...
# 5  
Old 01-09-2012
@Furou:

Something like this, not tested but I hope you get the idea:
Code:
grep { -d && m|^(?i)$before_last/$last$| } glob qq[*/*];

Regards,
Birei
This User Gave Thanks to birei For This Post:
# 6  
Old 01-09-2012
Quote:
Originally Posted by Furou
even if I always get the parameters in the same case, I still need to find out in what case exactly that folder is written in, because I need to transfer files there. Otherwise I would've used a lower case comparison like you suggested Corona688.
You can still compare them in lower case, just use the unconverted mixed-cased names from readdir after you've done so.

This will make it ambiguous when more than one identical name with different cases exists. Whichever comes first in the arbitrary listing order wins. I don't think you can avoid that.
This User Gave Thanks to Corona688 For This Post:
# 7  
Old 01-09-2012
Ok. Thanks to the both of you Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Uppercase to lowercase

Hello, I have a list of files in a directory whose names are all in uppercasse, including the file format for eg *.MP3 . I would like to convert these to the normal way we write it ie ABC.MP3 to be converted to Abc.mp3 . I know that this can be done manually by using a lot of "mv" or rename... (6 Replies)
Discussion started by: ajayram
6 Replies

2. Shell Programming and Scripting

how to convert user input to uppercase

Hi, how to convert user input (lowercase) to uppercase in the dos batch file ? echo. SET /p user1=Enter username: SET user2=%user1%V echo. echo %user1% echo. echo %user2% echo. With Regards (7 Replies)
Discussion started by: milink
7 Replies

3. UNIX for Dummies Questions & Answers

Validating user input

I'm trying to set up a script that takes user input and validates that the user input was entered correctly. So far I have this: while : do echo "Please enter your name." read NAME if then echo "You have not entered a name." echo... (13 Replies)
Discussion started by: fufaso
13 Replies

4. UNIX for Dummies Questions & Answers

UPPERCASE to lowercase

Hi All, i have a file and i want to convert all uppercase letters to lowercase letters which are in my file. how can i do this. Thanx (3 Replies)
Discussion started by: temhem
3 Replies

5. UNIX Desktop Questions & Answers

Unix: lowercase to uppercase

I just started to learn unix... and i needed to make a basic script. i need to 1. read a file (.txt) 2. count the words of EVERY sentece 3. sentences with odd number of words need to be converted into lowercase sentences with even number of words need to be converted into uppercase ... (6 Replies)
Discussion started by: chilli1988
6 Replies

6. Shell Programming and Scripting

indentation and lowercase to uppercase

hi, i need to write a bash script that does two things. the program will take from the command line a file name, which is a C code, and an integer, which is the size of my indentation i would then have to indent every nested code by the number of columns provided by the user in the... (1 Reply)
Discussion started by: kratos.
1 Replies

7. UNIX for Dummies Questions & Answers

uppercase to lowercase

i have no variable and no file i just want to convert AJIT to ajit with some command in UNIX can anybody help (4 Replies)
Discussion started by: ajit.yadav83
4 Replies

8. AIX

Lowercase to Uppercase

Inside a script I have 2 variables COMP=cy and PT=t. further down the same script I require at the same line to call those 2 variables the first time uppercase and after lowercase ${COMP}${PT}ACE,${COMP}${PT}ace. Can somebody help me Thanks in advance George Govotsis (7 Replies)
Discussion started by: ggovotsis
7 Replies

9. Shell Programming and Scripting

Validating user input is not blank

Trying to create a script in BASH that would ask the user to enter another user name making sure the input is not blank before they hit enter then to check the home directory of that user does exist, I have the check folder sorted it's just the loop to make sure the user has entered chars (5 Replies)
Discussion started by: MBN
5 Replies

10. Shell Programming and Scripting

uppercase to lowercase

Greetings & Happy New Years To All! A client of mine FTP'ed their files up to the server and it all ended up being in UPPERCASE when it all should be in lowercase. Is there a builtin command or a script anyone knows of that will automagically convert all files to lowercase? Please advise asap... (4 Replies)
Discussion started by: webex
4 Replies
Login or Register to Ask a Question