002#验证输入-仅限字母数字

确保输入内容仅限于字母和数字

#!/bin/bash
# validAlpNum.sh
# 验证输入,仅限字母数字

validAlphaNum() {

  validChar="$(echo $1 | sed -e 's/[^[:alnum:]]//g')"
  if [ "$validChar" = "$1" ]; then
    return 0
  else  
    return 1
  fi
}

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
View Code

 

posted @ 2020-05-19 15:42  bigtree2pingping  阅读(108)  评论(0编辑  收藏  举报