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:

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!

Works for me!

# 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
# ./test_cases.sh
12345 - Numeric
BI125 - Alphanumeric
BIPIN - Uppercase
BiPiN - Mixed
bipin - Lowercase
Bipin - Mixed

A refinement:

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

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.

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.

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.

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!

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.

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:

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.

1 Like

Though not finished yet, this is what I am using:

echo -e read "enter name"
read netwrk
for DIR in $(find /usr/$netwrk -type d -prune | sed 's/\.\///g');do if expr match "$netapp" "[[:lower:]]$" > /dev/null; then echo "$netapp - Lowercase
"; elif expr match "$netwrk" "[[:upper:]]
$"; then echo "$netwrk - Uppercase";fi;done>/tmp/filea
export varz=`echo $netwrk |tr [a-z] [A-Z]`
for DIR in $(find /var/$varz -type d -prune | sed 's/\.\///g');do if expr match "$varz" "[[:lower:]]$" > /dev/null; then echo "$varz - Lowercase
"; elif expr match "$varz" "[[:upper:]]
$"; then echo "$varz - Uppercase";fi;done>/tmp/fileb

if [[ -s /tmp/filea && -s /tmp/fileb ]]; then echo "you need to try both lower and upper case"; elif [[ -s /tmp/filea ]]; then echo "you're done";fiI am trying to work with user input and see if a directory is both upper and lower case and the above is actually working. I am going to shorten it as it does not need to be so long and there are some unneeded lines. I was able to successfully detect if the directory was there or wasn't there using this. I am very thankful for your posts as I do believe it was the fact that I was using this user-input method that the other scripts did not work. As the user would only type in once a directory in upper or lower case the scripts would check to see if that upper case or lower case directory was there but of course not both upper and lower as the user only typed the name once in ONE case. Again, thanks so much, especially to Corona! I am going to revise the above and also have it remove filea and b when the script is done. I am already successfully using the above script and my problem is solved. You've lead this noob to see the error of their ways and how to better explain things! And you guys are right about putting code on more than one line, I will have to let my superstitions go and do that!