首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 操作系统 > UNIXLINUX >

怎么在shell脚本里用正则表达式限制输入格式

2014-01-14 
如何在shell脚本里用正则表达式限制输入格式echo -ne \n$MESSAGE for $THEUSER (minimum 8 chars. with o

如何在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 $password 
[解决办法]
 egrep '[a-z]{7,}([0-9]
[解决办法]
[!@#$%&^*]){1}' 1>/dev/null;then
               echo "your password is invalid"
  fi



厉害。佩服佩服。

热点排行