Example No. 1
As a shell writer, to avoid the user's input data is malformed, that is, to intercept the possible errors before the shell script is published, this example is to determine whether the data (or positional variables) entered by the user is available, and then invoke the code block when the condition is met;
#! / bin / bash
#shell name: shell_vaild.sh
#used: Determine whether the password is composed of numbers and letters after entering the password, determine whether the entered IP is legal, etc.
#author by woon
#Definition function
vaild_pass ()
{
decide = $ (echo $ 1 | sed -e "s / [^ [: alnum:]] // g")
if ["$ decide"! = "$ pass"]; then
return 1
else
return 0
fi
}
#
read -t 10 -p "Please input IP address:" ipaddr
read -s -p "Please input the password:" pass
echo
#Determine if the input is empty; empty to exit the program
if [-z $ ipaddr] || [-z $ pass]; then
echo "Wrong input \ n"
exit 2
fi
if! vaild_pass "$ pass"; then
echo "Your inpur must consist of only letters and numbers."> & 2
else
echo "Your password is vaild!"
fi
yn = $ (echo $ ipaddr | awk -F "." ‘{if ($ 1 <255 && $ 2 <255 && $ 3 <255 && $ 4 <255) {print" 0 "} else {print" 1 "}}}))
if [$ yn == "0"]; then
echo "Your Ipaddr is vaild"
else
echo "Your IPADDR is not vaild."
exit 1
Linux Shell Example (iii)