Skip to content

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:

#!/bin/bash

echo -n "Enter alphanumeric input: "
read input
 
compressed="$(echo $input | sed -e 's/[^[:alnum:]]//g')"
 
if [ "$compressed" != "$input" ] ; then
    echo "Input not valid."
else
    echo "Input valid."
fi

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:

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.

{ 5 } Comments

  1. David Tomaschik using Mozilla Firefox Mozilla Firefox 2.0.0.11 on Ubuntu Linux Ubuntu Linux | January 3, 2008 at 10:24 pm | Permalink

    I think this might be simpler:

    if echo $input | grep -q ‘[^0-9A-Za-z]‘
    then echo “Input not valid.”
    else
    echo “Input valid.”
    fi

    [Reply]

  2. Tormod Volden using Mozilla Firefox Mozilla Firefox 2.0.0.11 on Ubuntu Linux Ubuntu Linux | January 4, 2008 at 5:51 am | Permalink

    Or better:
    if echo $input | egrep -q “\W”

    [Reply]

  3. Dave Taylor using Safari Safari 523.10.6 on Mac OS Mac OS X | January 4, 2008 at 6:44 am | Permalink

    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. :-)

    [Reply]

  4. Aaron using Mozilla Firefox Mozilla Firefox 2.0.0.11 on Ubuntu Linux Ubuntu Linux | January 4, 2008 at 7:36 am | Permalink

    @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.

    [Reply]

  5. Gareth Williams using Mozilla Firefox Mozilla Firefox 3.0b5 on Linux Linux | May 12, 2008 at 6:11 am | Permalink

    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.
    :-)

    [Reply]

Post a Comment

Your email is never published nor shared. Required fields are marked *

Powered by WP Hashcash