假设你说的“字符”指的是字母或数字 (a-z A-Z 0-9),如果不是,可以修改 grep 命令的正则表达式:
function check_all_letter
{
matchStr=`echo ${1} | grep -o "^[a-zA-Z0-9]*"`
if [ "${matchStr}" == "${1}" ]; then
echo "input correct."
else
echo "error, include inlegal character !"
fi
}
使用方法例子:
read -p "input content: " input
check_all_letter ${input}
read string
echo $string|egrep '[[:alpha:]]'
if $0;then
echo success
else
echo failure
fi