Matching a choice of character in test command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Matching a choice of character in test command
# 1  
Old 08-21-2007
Matching a choice of character in test command

Hi
[ "$answer" != [Yy] ] && exit 0
Although it, the $answer, is 'y', the test operation returns true.

[ "$answer" != y ] && exit 0

This works but I want to do multiple choice matching.
I don't want to do like:
[ "$answer" != y -a "$answer" != Y]

Please help
# 2  
Old 08-21-2007
What shell are you using? Bash allows you to use something like
Code:
[[ $answer =~ [^yY] ]] && exit 0

The =~ operator treats the string to its right as a regex and will do that kind of matching for you. I don't think that this is supported by most shells though.
# 3  
Old 08-21-2007
Or...
Code:
case $answer in [Yy]) exit 0 ;; esac

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Test command with special character not work

Hi all, Case 1 : A=88^M && echo "PASS" Result: PASS Case 2: A=88 && echo "PASS" Result: PASS I would like to know why Case 1 and Case 2 got the same result? What make ^M ignored ? Thanks in advance. (6 Replies)
Discussion started by: montor
6 Replies

2. UNIX for Dummies Questions & Answers

tcsh - test for character in string for if then

I apologize if this treads already covered ground (which I sure it must have), however I wasn't able to determine what I needed from searches. I'm trying to do detemine if a string represents a file name or not (i.e., is in the form "string.ext" or just "string"), by seeing if there's a period... (2 Replies)
Discussion started by: deepstructure
2 Replies

3. Shell Programming and Scripting

pattern matching on any special character in Unix

Hi, I have field in a file which would come with any special character, how do i check that field? Eg: @123TYtaasa>>>/ 131dfetr_~2 In the above example, how do I add pattern for any special character on the keyboard. Thanks (3 Replies)
Discussion started by: techmoris
3 Replies

4. Shell Programming and Scripting

Matching multiples of a single character using sed and awk

Hi, I have a file 'imei_01.txt' having the following contents: $ cat imei_01.txt a123456 bbr22135 yet223 where I want to check whether the expression 'first single alphabet followed by 6 digits' is present in the file (here it is the first record 'a123456') I am using the following... (5 Replies)
Discussion started by: royalibrahim
5 Replies

5. UNIX for Dummies Questions & Answers

Matching character

Alright, I am stuck here. I have this variable that stores the word = HELLO and I have converted it it to ----- I have asked user to input one character at a time. SAy, if they enter E. Therefore, I need to search 2nd character and input E there. makes it -E--- (other checkings have been... (2 Replies)
Discussion started by: felixwhoals
2 Replies

6. Shell Programming and Scripting

tcl: regexp matching special character

Hello Experts, Can someone help me here: I have a variable which contains a string with "". set var1 {a} set str1 {a is the element i want to match} Now "regexp $var1 $str1" does not work? ("regexp {a\} $str1" works, but var1 gets it's value automatically from another script) Is... (6 Replies)
Discussion started by: sumitgarg
6 Replies

7. Shell Programming and Scripting

matching repeated character

looking for a bit of help with sed. I have a file that looks a bit like this: sdfghhjk asdfdfghgj asdfhgghj werdfvtfh edftbgh 1211211221 sdffgfm dfghnhjm dfvfsgbgh adsfv bdhgn 1111111dffg dfv1122 dsgvbghn111111 fffffffgbdghn fffffff sfgh3333gs vdf (5 Replies)
Discussion started by: robsonde
5 Replies

8. Shell Programming and Scripting

Test return character.

Hi, in my korn shell I have this code: typeset -uL1 rc read rc?"Insert Y=Yes (default) or N=No >>" If I press enter without value I wish to set rc=Y. This is my default. This test: if ] then .... Do not work. I hope in your help. Thanks in advance. Regards, Giovanni (3 Replies)
Discussion started by: gio123bg
3 Replies

9. UNIX for Dummies Questions & Answers

character matching

Is there a way to pull out a character at a time from a work in unix, using a shell script? (1 Reply)
Discussion started by: Rukshan
1 Replies
Login or Register to Ask a Question