确保输入内容仅限于字母和数字
#!/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