Bash Shell Script to check if a string has only alphabets and digits.
Hello,
I am trying to write a simple shell script. Here I want to pass a string to the script and want the script to check if the string has only alphabets and digits. Code:
#!/bin/bash best wishes, Aswani |
you are comparing $Rand_String to the literal string "[0-9A-Za-z]*". If you want that to be interpreted as a pattern, you need to remove the double quotes. Also, bash globs are not regexp (at least not by default) so your pattern does not mean "an arbitrary number of alphanumeric characters", but "an alphanumeric character followed by arbitrary number of any characters." One way to do what you want might be something like this:
Code:
[[ "${Rand_String//[0-9A-Za-z]/}" = "" ]] && echo "Only contains alphanumeric" || echo "Contains other characters" |
#!/bin/bash
# This is one solution: # 1) don't use test, it has very limited capabilities # 2) added quoting to allow spaces in input string # 3) use regular expression operator =~ # 4) use ^ "not in set" regexp operator # 5) use + for one or more matches rather than * for zero or more Rand_String="$1" if [[ "$Rand_String" =~ [^0-9A-Za-z]+ ]] ; then echo "string $Rand_String has characters which are not alphanumeric" else echo "string $Rand_String has alphabets which are only alpha numeric" fi |
How about this instead?
Code:
case ${string//[[:alnum:]]} in Edit: a variation on the theme, perhaps easier to read: Code:
case $string in |
Strictly speaking, the pattern needs to also include the beginning-of-string and end-of-string anchors as well. The pattern must be: "beginning-of-string, followed by zero-or-more occurrences of, say, :alnum:, followed by end-of-string." You might or might not need to use egrep to specify this "extended" regular expression.
One of the best things you can do is to spend a lot of time patiently studying the arcane art of regular-expressions ... which are actually "easy to understand but dammed hard to read." They are one of the great power-tools of the programming craft. Although the "string of gibberish" presentation of a regex is fairly confrontational, and the facility is full of (frankly, too many) arcane options, the concept is a simple one, and there are enormous very-practical applications for it. The knowledge will serve you very, very well. Oh yes, in the Windows world too. |
Quote:
However please note that when I remove the double quotes in my original script as mentioned by you, the script gives an error saying Code:
[aswani@maruthi]$ test_string.sh abcd |
Quote:
That's not really correct here. All regex searches start implicitly from the beginning of the line. And the only thing we're interested in is whether there's at least one non-alphanumeric character as it walks towards the end in its search. So we don't even really need a repeater character here. All the "[]" bracket expression has to do is find a single a matching character and the regex returns true, otherwise false. Globbing, BTW, is implicitly anchored at both ends, so the expression must take into account the whole string. Code:
[[ $string =~ [^[:alnum:]] ]] && echo "not clean" || echo "clean" Code:
re='[^[:alnum:]]' |
Quote:
|
Quote:
|
All times are GMT -5. The time now is 01:08 AM. |