Trying to test for both upper and lower case directories


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Trying to test for both upper and lower case directories
# 1  
Old 12-13-2012
Trying to test for both upper and lower case directories

I am trying to get a script to print out whether a directory is lowercase uppercase or both. This is what I've got so far:

Code:
echo -e read "enter name"
read server
for DIR in $(find /tmp/$server -type d -prune | sed 's/\.\///g');do if expr match "$server" "[a-zA-Z]*$" > /dev/null; then echo "$server - Both"; elif expr match "$server" "[[:upper:]]*$"; then echo "$server - Uppercase"; elif expr match "$server" "[[:lower:]]*$" > /dev/null; then echo "$server -there are lower";fi;done

Now this will let me know whether either an upper or lowercase directory is present for the servername and also if both are present, like this

/tmp/server -lowercase present
/tmp/SERVER - uppercase present.
if both are present, it prints both

However, if I type in a lowercase name and there is only a lowercase directory, it still prints "both." What am I doing wrong? I think "[a-zA-Z]* is bad because it matches incorrectly. I think I need something like "[[:upper:]]*$ && [[lower:]]" but this does not work.

Any help would be greatly appreciated!
Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 12-13-2012 at 09:53 PM.. Reason: code tags, please!
# 2  
Old 12-13-2012
Works for me!
Code:
# cat test_cases.sh
#!/bin/bash

for DIR in $( find . -type d | sed 's/\.\///g' )
do
        if expr match "$DIR" "[[:lower:]]*$" > /dev/null
        then
                echo "$DIR - Lowercase"
        elif expr match "$DIR" "[[:upper:]]*$" > /dev/null
        then
                echo "$DIR - Uppercase"
        elif expr match "$DIR" "[0-9]*$" > /dev/null
        then
                echo "$DIR - Numeric"
        elif expr match "$DIR" "[a-zA-Z]*$" > /dev/null
        then
                echo "$DIR - Mixed"
        elif expr match "$DIR" "[a-zA-Z0-9]*$" > /dev/null
        then
                echo "$DIR - Alphanumeric"
        fi
done

Code:
# ./test_cases.sh
12345 - Numeric
BI125 - Alphanumeric
BIPIN - Uppercase
BiPiN - Mixed
bipin - Lowercase
Bipin - Mixed

# 3  
Old 12-13-2012
A refinement:

Code:
find . -type d | sed 's/[^a-zA-Z]//g' | while read DIR
do
        case "$DIR" in
        *[a-z][A-Z]*|*[A-Z][a-z]*)        echo "$DIR Mixed" ;;
        *[A-Z]*)        echo "$DIR Uppercase" ;;
        *[a-z]*)        echo "$DIR Lowercase" ;;
        esac
done

# 4  
Old 12-14-2012
Issue is with lower /uppercase variable

I think why what I have is not working is because the input that the user enters will only be uppercase or lowercase not both. So when my script checks the directories, it says yes there is uppercase when you enter the uppercase input and yes there is lowercase when you enter the lowercase input.

But then since the user does not enter both upper and lowercase input, the script will never say both. Is there an easy way to get the script to convert the input into uppercase while also checking for the lowercase name? Or am I off track here? Any help is appreciated. Also, thanks for your previous responses.
# 5  
Old 12-14-2012
Why will the user never enter uppercase and lowercase? What is preventing them?

And how would forcing the string to uppercase or lowercase prevent it from being all lowercase or all uppercase?

And what is this script of yours that's not working? It must be far different by now, and we can't see it from here.
# 6  
Old 12-14-2012
Variable problem? Still can't both to return

Corona:

This script is what I have. It does identify both lower and upper case but will never print "both." For example, if I enter david as the input it responds david -lowercase. If I enter DAVID as the input it responds DAVID- uppercase. But if I type either david or DAVID it never prints both. But in reality there are both DAVID and david directories in /tmp such as this:

/tmp/david
/tmp/DAVID

I realize this may be a dumb question on my part, but it appears to me that the reason this script does not return the "both" value is that I am entering only lowercase or uppercase into the input and it is being read as such and not looking to see if there are the two directories.

Code:
echo -e read "enter name"
read netapp
for DIR in $(find /usr/openv/netbackup/db/images/$netapp -type d -prune | sed 's/\.\///g');do if expr match "$netapp" "[a-zA-Z]*$" > /dev/null; then echo "$netapp - Both"; elif expr match "$netapp" "[[:upper:]]*$"; then echo "$netapp - Uppercase"; elif expr match "$netapp" "[[:lower:]]*$" > /dev/null; then echo "$netapp -there are lower";fi;done

Thanks again for your patience with my newbie questions!

Last edited by Corona688; 12-14-2012 at 12:56 PM..
# 7  
Old 12-14-2012
You do realize you don't have to jam it all on one line? You can put a newline wherever you have a ; and immediately after pipes too.

Code:
echo -e read "enter name"
read netapp
for DIR in $(find /usr/openv/netbackup/db/images/$netapp -type d -prune |
        sed 's/\.\///g')
do
        if expr match "$netapp" "[a-zA-Z]*$" > /dev/null
        then
                echo "$netapp - Both"
        elif expr match "$netapp" "[[:upper:]]*$"
        then
                echo "$netapp - Uppercase"
        elif expr match "$netapp" "[[:lower:]]*$" > /dev/null
        then
                echo "$netapp -there are lower"
        fi
done

I suggest trying my code from earlier, with some slight changes now that it's clearer what you want:

Code:
find /usr/openv/netbackup/db/images/$netapp -type d -prune | while read DIR
do
        # convert /path/to/abc9DEF into abcDEF
        NAME=$(basename "$DIR" | sed 's/[^a-zA-Z]//g')
        case "$NAME" in
        *[a-z][A-Z]*|*[A-Z][a-z]*)        echo "$DIR Mixed" ;;
        *[A-Z]*)        echo "$DIR Uppercase" ;;
        *[a-z]*)        echo "$DIR Lowercase" ;;
        esac
done

How it works: Take a name like 'abc9DEF' and strip all non A-Z from it, so you get abcDEF.

Then check for an uppercase letter followed by a lowercase one, or vice versa. If you find that, it's mixed case. Otherwise, it's either all-upper or all-lower.

Last edited by Corona688; 12-14-2012 at 01:04 PM..
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 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. UNIX for Dummies Questions & Answers

To convert Lower case to Upper Case

There is a script where we pass the parameter in lower case: say: . ./scriptName pArameter #!/bin/ksh echo "`date` Entering $0 Reloading the $1 table " mname1=$1 (code to login MYSQL Database) Truncate table $mname1; exit ! Since now there is a limitaion of MYSQL that it accept... (5 Replies)
Discussion started by: ambarginni
5 Replies

3. Shell Programming and Scripting

converting to lower case or upper case

here is a code column_name="vivek" column_name2="ViVeK" column_name=$(echo $column_name | awk '{print tolower($0)}') column_name2=$(echo $column_name2 | awk '{print tolower($0)}') echo "column name 1 lower: $column_name" echo "column name... (6 Replies)
Discussion started by: vivek d r
6 Replies

4. 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

5. 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

6. 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

7. 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

8. 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

9. Shell Programming and Scripting

how to convert value in a variable from upper case to lower case

Hi, I have a variable $Ctrcd which contains country names in upper case and i want to convert them into lower case. I have tried so many solutions from already existing threads but couldn't get the correct one. Can anybody help me with this..... Thanks a lot.. (2 Replies)
Discussion started by: manmeet
2 Replies

10. 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