日期:2014-05-16  浏览次数:20728 次

如何在shell脚本里用正则表达式限制输入格式
echo -ne "\n$MESSAGE for $THEUSER (minimum 8 chars. with one numeric/special char.) : "
stty -echo
read THEPASSWORD
stty echo

读进来的THEPASSWORD 如何符合上面的要求呢,如何检查?

------解决方案--------------------
THEPASSWORD=ladofwind@123
n=0
echo "$THEPASSWORD" 
------解决方案--------------------
 grep -q "........"
if [ $? == 0 ]; then
    echo [minimum 8 chars] yes
    n=`expr $n + 1`
else
    echo [minimum 8 chars] no
fi
echo "$THEPASSWORD" 
------解决方案--------------------
 grep -q "[0-9]"
if [ $? == 0 ]; then
    echo [one numeric] yes
    n=`expr $n + 1`
else
    echo [one numeric] no
fi

echo "$THEPASSWORD" 
------解决方案--------------------
 grep -q "[^0-9a-zA-Z]"
if [ $? == 0 ]; then
    echo [one special] yes
    n=`expr $n + 1`
else
    echo [one special] no
fi
if [ $n == 3  ]; then
    echo password valid
else
    echo password invalid
fi

------解决方案--------------------

#!/bin/sh

read -p 'Set your password(your password must contain a minimum of 8 chars with one numeric/special char.) ' password

  if ! echo $password 
------解决方案--------------------
 egrep '[a-z]{7,}([0-9]
------解决方案--------------------
[!@#$%&^*]){1}' 1>/dev/null;then
               echo "your password is invalid"
  fi



------解决方案--------------------
引用:

#!/bin/sh

read -p 'Set your password(your password must contain a minimum of 8 chars with one numeric/special char.) ' password

  if ! echo