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

ladofwind 2013-12-27 03:11:24
echo -ne "\n$MESSAGE for $THEUSER (minimum 8 chars. with one numeric/special char.) : "
stty -echo
read THEPASSWORD
stty echo

读进来的THEPASSWORD 如何符合上面的要求呢,如何检查?
...全文
199 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
bblxl2008 2013-12-30
  • 打赏
  • 举报
回复
引用 2 楼 freefallben 的回复:

#!/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

厉害。佩服佩服。
freefallben 2013-12-28
  • 打赏
  • 举报
回复

#!/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

ljc007 2013-12-27
  • 打赏
  • 举报
回复
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

19,610

社区成员

发帖
与我相关
我的任务
社区描述
系统使用、管理、维护问题。可以是Ubuntu, Fedora, Unix等等
社区管理员
  • 系统维护与使用区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧