How to determine if a string contains some characters
I need an if statement for a Linux bash shell script, to determine if a string contains one or more of a given set of characters. For example:
randstring=caboosel1 I want to know if this string contains any of the following: 1 (one), l (lower case L), 0 (zero) or O (upper case O). Thanks in advance... |
Assume your string is in a variable "mystring"
echo $mystring|grep [abxy] This will echo the string if any one of the four characters is present. You should be able to put this inside an if statement. Note that you cannot use the grep return code in the above construct (I'm not sure why---if you do "grep stuff filename" you get a return code of 1 (false) if nothing is found) |
Code:
$ cat example |
Since version 3 of bash you can use also the regular expression match. Following the example by PTrenholme:
Code:
$ cat example |
oobash
Try oobash. It is an oo-style string library for bash 4. It has support for German umlauts. It is written in bash. Many functions are available: -base64Decode, -base64Encode, -capitalize, -center, -charAt, -concat, -contains, -count, -endsWith, -equals, -equalsIgnoreCase, -reverse, -hashCode, -indexOf, -isAlnum, -isAlpha, -isAscii, -isDigit, -isEmpty, -isHexDigit, -isLowerCase, -isSpace, -isPrintable, -isUpperCase, -isVisible, -lastIndexOf, -length, -matches, -replaceAll, -replaceFirst, -startsWith, -substring, -swapCase, -toLowerCase, -toString, -toUpperCase, -trim, and -zfill.
Look at the contains example: [Desktop]$ String a testXccc [Desktop]$ a.contains tX true [Desktop]$ a.contains XtX false http://sourceforge.net/projects/oobash/ |
All times are GMT -5. The time now is 07:49 PM. |