Test command non case specific string comparision

Hi,

I want to do caseless string comparision using test command

for eg: Ind_f="y"

test "$Ind_f" == "y|Y"

i tried [yY] , *[yY]* [y|Y] , nothing worked.

any thoughts on how to do case insensitive string comparison using test command without converting to any particular case using typeset or tr?

why are you sticking to test command?

this could help

Here is a sample script that shows a few ways to do what you're trying to do that work with both bash and ksh:

printf "Enter answer: "
while read answer
do      if [ "$answer" = "y" ] || [ "$answer" = "Y" ]
        then    printf "true: %s\n" \
                        'if [ "$answer" = "y" ] || [ "$answer" = "Y" ]'
        else    printf "false: %s\n" \
                        'if [ "$answer" = "y" ] || [ "$answer" = "Y" ]'
        fi
        if [[ "$answer" == [yY] ]]
        then    printf "true: %s\n" \
                        'if [[ "$answer" == [yY] ]]'
        else    printf "false: %s\n" \
                        'if [[ "$answer" == [yY] ]]'
        fi
        if [[ "$answer" == [yY]* ]]
        then    printf "true: %s\n" \
                        'if [[ "$answer" == [yY]* ]]'
        else    printf "false: %s\n" \
                        'if [[ "$answer" == [yY]* ]]'
        fi
        case "$answer" in
        ([yY])  printf "match: case %s\n" '([yY])';;
        ([nN])  printf "match: case %s\n" '([nN])';;
        (*)     printf "no match: cases ([yY]) and ([nN])\n"
        esac
        case "$answer" in
        ([yY]*) printf "match: case %s\n" '([yY]*)';;
        ([nN]*) printf "match: case %s\n" '([nN]*)';;
        (*)     printf "no match: cases ([yY]*) and ([nN]*)\n"
        esac
        printf "Enter another answer: "
done
printf "\nDone.\n"

The [[ expr ]] tests are not available in a Bourne shell and are not required by the POSIX standards; the other forms are required by the POSIX standards and will also work with an old Bourne shell.

1 Like

Hi Don,

Thanks for your sample script. So if I understand it right, caseless comparision cannot be done just by any combinations of [Yy] right?

Only way to do using test command or single brace [] is to use or operator and no combination of [Yy] would work?

is that right?

Thanks.

Yes. According to the standards, test's = operator compares strings; not filename matching patterns and not regular expressions. The test utilities in some shells support operators and expressions in addition to those specified by the standards, but they aren't portable.

The [[ expression ]] forms aren't in the standards yet, but are being considered for inclusion in the next revision of the POSIX standards and the Single UNIX Specification.

1 Like

Hi Don...
"$aswer" ?

printf "Enter answer: "
while read answer
do      if [ "$answer" = "y" ] || [ "$answer" = "Y" ]
        then    printf "true: %s\n" \
                        'if [ "$answer" = "y" ] || "$answer" = "Y" ]'
        else    printf "false: %s\n" \
                        'if [ "$answer" = "y" ] || "$answer" = "Y" ]'

Cut-n-paste error I realise, but it threw me for a couple of minutes...

1 Like

Yes. Obviously. Thanks for catching it. I have corrected the sample code in my earlier post. :o

1 Like

Hi Don...

No problem but I hate to say this but there might be more:-

Same lines, but missing " [" just after "||" on two of them...

I missed that first time round...

1 Like

Yes, again. At least these were just in the printf arguments; not in the code to be executed. :o

I've go to stop answering simple problems like this in the middle of the night and start getting more sleep. (Is it New Year's resolution time?)

1 Like