当前位置:网站首页>Shell input validation alphanumeric only

Shell input validation alphanumeric only

2022-06-26 06:57:00 My talented girlfriend

#!/bin/bash #  Allow text to execute as a script , Interpreter 

valiaAlphaNum(){
    
	validchars="$(echo $1 | set -e 's/[^[:alnum:]]//g')" 
	# Be sure to pay attention to shell Assignment statement for = There is no space on the left and right sides of the sign 
	# set Put variables in it like a channel ,-e  Multiple matches  [:alnum:]  Represents letters and numbers   Sometimes it will lead to : Disappear , Write normally 
	if [ "$validchars"="$1" ]; then
		return 0
	else
		return 1
	fi
}

/bin/echo -n "Enter input:"
read input
#  Read the input characters 
if ! valiaAlphaNum "$input" ; then
#  Notice the !  Number   and  valiaAlphaNum  There's a space between 
	echo "you input must consist of only letters and numbers.">&2
	exit 1
else
	echo "input is valid."
fi

exit 0
原网站

版权声明
本文为[My talented girlfriend]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206260641599900.html