Testing AlphaNumeric Arguments In Bash

Spending the evening working on my shell scripting, I thought I would jump into "Wicked Cool Shell Scripts" by Dave Taylor. In his script validalnum.sh, he has a test case to check if a user entered in valid alphabetic or numeric characters. His result is elegant and clean. I've changed up the script a bit for clarity:

In this example, the user is asked to enter input that can be any combination of letters and numbers, regardless of case. If the user enters punctuation, the test case fails, and the user is notified of such. Otherwise, the test case passes, and everyone is happy.

I want to call to your attention the cornerstone of this script, however:

1

compressed="$(echo $input | sed -e 's/[^[:alnum:]]//g')"

The variable $compressed is holding only alphanumeric characters. This is done by taking the user input, and piping it to the stream editor sed. With sed, we are searching for any character in the string that is not a number or a letter. If such a character exists, we remove the character altogether. Thus, if $compressed removes any characters, then it does not match what the user entered, and our test will fail. If no characters were removed, then no punctuation exists in the input, and our test case will pass.

I thought this was most clever, and just had to share, hoping others benefit from this simple example. I also hope that Dave is not mad at me for taking an example, changing it up a bit, and presenting it on this blog. Thanks Dave.

How can I be upset when you refer to my script as "elegant and clean"? Seriously, I love to see people further hack and push the bits around. Trust me, I'm not the world's best shell script writer, I just have persistence.

@David Tomaschik and Tormod Volden- The idea isn't simplicity, but elegance and flexibility. Dave Taylor's solution brings more to the table, and let's the script writer take advantage of the $compressed variable if needed.

@Dave Taylor- Welcome! I was worried that the code may be copyrighted or otherwise unavailable for redistribution. So, as you may have noticed, I changed the script considerably "just in case". Anyway, it's a great book, and I'm looking forward to learning more from it.

Very useful discussion - taught me a few things as a beginner:
1) don't cut and paste the examples from the comments into a script as they won't work - the quote marks need to be typed as proper single or double quotes!
2) the "\W" example works differently from the other two as it will allow underscore in the input text.

Based on Aarons code, we can make a slight modification of the INPUT variables value. Basically, it allows a user to match only alpha numeric input, but with inclusion of the $(echo $1 | sed -e 's/_//g') part, we can make exceptions to the [^[:alnum:]] part.

The final result is such that the function will allow only alpha numeric input and characters specified in the INPUT variables sed part.

Broken down:
1) invoke the function: validate_input
2) use sed to remove the underscore from blah_blah and set the result as the variable INPUT
3) use sed to restrict the VALID variable to alphanumeric characters
4) compare the two, if they differ, report that there are invalid characters present.

One can thus restrict the valid input to only alphanumeric characters and other user specified characters... (in our case, the underscore)