Linux Shell Example (iii)

Source: Internet
Author: User


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)


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.