Test command non case specific string comparision


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Test command non case specific string comparision
# 1  
Old 12-27-2013
Test command non case specific string comparision

Hi,

I want to do caseless string comparision using test command

for eg: Ind_f="y"

Code:
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?

Last edited by Don Cragun; 12-27-2013 at 01:04 PM.. Reason: Add CODE and ICODE tags.
# 2  
Old 12-27-2013
why are you sticking to test command?

this could help
# 3  
Old 12-27-2013
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:
Code:
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.

Last edited by Don Cragun; 12-30-2013 at 10:48 PM.. Reason: Fix typos: $aswer in a few places should have been $answer & missing "[" in printf arguments.
This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 12-30-2013
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.
# 5  
Old 12-30-2013
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.
This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 12-30-2013
Hi Don...
"$aswer" ?
Code:
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...
This User Gave Thanks to wisecracker For This Post:
# 7  
Old 12-30-2013
Quote:
Originally Posted by wisecracker
Hi Don...
... ... ...
Cut-n-paste error I realise, but it threw me for a couple of minutes...
Yes. Obviously. Thanks for catching it. I have corrected the sample code in my earlier post. Smilie
This User Gave Thanks to Don Cragun 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

How to test that a string doesn't contain specific characters?

Hi ! :) I made this : #!/bin/bash rsa_dir="/etc/openvpn/easy-rsa/" rsa_key_dir="/etc/openvpn/easy-rsa/keys/" ccd_dir="/etc/openvpn/ccd/" regex_special_char='' cd $rsa_dir while read -p "Please can you enter the vpn's username : " username ] || ] || ] || ] do echo "Your entry... (10 Replies)
Discussion started by: Arnaudh78
10 Replies

2. Shell Programming and Scripting

Unable to update the string in a file trough case command

Hi All, my requirement is first search the line and updated stg value with the user input value. ws.rsp.url=https://rt930.rsp-stg.cb.info53.com/RSP/RAFT^M stg is not fixed string it may varies.So i used the below command for it rsp=`cat properties | grep ^ws.rsp.url= | awk -F"/"... (1 Reply)
Discussion started by: bhas85
1 Replies

3. Shell Programming and Scripting

Prefixing test case methods with letter 'test'

Hi, I have a Python unit test cases source code file which contains more than a hundred test case methods. In that, some of the test case methods already have prefix 'test' where as some of them do not have. Now, I need to add the string 'test' (case-sensitive) as a prefix to those of the... (5 Replies)
Discussion started by: royalibrahim
5 Replies

4. Shell Programming and Scripting

String Comparision

I want to compare two strings using awk dynamically without trimming the spaces and want to find the count of matching string. Input Strings file: File1 content (file1): " a " " a2 " File2 content (file2): " a " " a " " a2 " " b2 " " c2 "... (3 Replies)
Discussion started by: AhmedLakadkutta
3 Replies

5. Shell Programming and Scripting

String comparision

I have a string like ab or abc of whatever length. But i want to know whether another string ( for example, abcfghijkl, OR a<space> bcfghijkl ab<space> cfghijkl OR a<space>bcfghijkl OR ab<space> c<space> fghijkl ) starts with ab or abc... space might existing on the longer string... If so, i... (1 Reply)
Discussion started by: nram_krishna@ya
1 Replies

6. Shell Programming and Scripting

How to check weather a string is like test* or test* ot *test* in if condition

How to check weather a string is like test* or test* ot *test* in if condition (5 Replies)
Discussion started by: johnjerome
5 Replies

7. Shell Programming and Scripting

Test on string containing spacewhile test 1 -eq 1 do read a $a if test $a = quitC then break fi d

This is the code: while test 1 -eq 1 do read a $a if test $a = stop then break fi done I read a command on every loop an execute it. I check if the string equals the word stop to end the loop,but it say that I gave too many arguments to test. For example echo hello. Now the... (1 Reply)
Discussion started by: Max89
1 Replies

8. Shell Programming and Scripting

awk command to test if a string is a file

What awk command will test a string to determine if it is a valid file name? With the following awk statement I isolate the lines from the inputfile that might contain a filename, then I attempt to test the possible filename which is always in $4 from each line. However it is not working at all... (4 Replies)
Discussion started by: Arsenalman
4 Replies

9. Shell Programming and Scripting

case insenserive comparision

in If statement how can i campare "ASCII" with "ascii" the result of comparision shold be true..... (2 Replies)
Discussion started by: mahabunta
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