#!/bin/bash
# validAlphaNum - Ensures that input consists only of alphabetical
# and numeric characters.
# 检测输入是否只有数字和字母  
#
validAlphaNum()
{
  compressed="$(echo $1 | sed -e 's/[^[:alnum:]]//g')"   #替换掉特殊字符标点
  if [ "$compressed" != "$input" ] ; then
    return 1
  else
    return 0
  fi
}
# Sample usage of this function in a script
##########################
#1.输入
#########################
echo -n "Enter input: "
read input
if ! validAlphaNum "$input" ; then
  echo "Your input must consist of only letters and numbers." >&2 #您的输入必须只能包含字母和数字
  exit 1
else
  echo "Input is valid."  #输入有效
fi
exit 0

##################################################

测试:

[root@www ~]# ./check_input_alnum.sh 

Enter input: fdsadfa

Input is valid.

[root@www ~]# ./check_input_alnum.sh 

Enter input: dfaf,,.

Your input must consist of only letters and numbers.