Shell script test to authenticate Input

Source: Internet
Author: User

Shell script to test the legalization of input. Input: only numbers and letters are allowed. The description is often ignored and incorrect data is entered. As a Shell script developer, you must intercept and correct these errors. Typically, you may encounter file names or database keys. You are prompted to enter a string consisting of uppercase and lowercase letters and numbers, without punctuation, special characters, and spaces. Are they correct? This is the content to be tested by this script. 01validAlphaNum. sh02 # validAlphaNum. sh-make sure that the input only consists of numbers and letters 03 04 #! /Bin/sh05 06 validAlphaNum () 07 {08 # Legal parameter: all are uppercase letters, lowercase letters, and numbers, and 0 is returned; otherwise, returns 109 10 # delete all unacceptable characters 11 compressed = "$ (echo $1 | sed-e's/[^ [: alnum:] // G ') "12 13 if [" $ compressed "! = "$ Input"]; then14 return 115 else16 return 017 fi18} example usage of this function. The following code is directly added to validAlphaNum. sh: 01validAlphaNum. sh02 echo-n "input:" 03 read input04 05 if! ValidAlphaNum "$ input"; then06 echo "can only contain letters and numbers. "> & 207 exit 108 else09 echo" is valid. "10 fi11 12 exit 0 script analysis: the script logic is very clear. First, it converts the input to a brand new one using sed. Then compare the new data with the original one. If the two are identical, they are perfect. Otherwise, the data lost during conversion does not belong to the set of character numbers, so the original input is invalid. Sed is used to process data that is not in the POSIX character class [: alnum. This character class is a collection of all uppercase letters, lowercase letters, and numbers. If the data processed by sed does not match the previous input, the movement of all letters and numbers reveals the existence of non-numeric letters in the input string (invalid, then the function returns a non-0 value. Run the Script: This script is complete. It prompts you to enter the information and determines whether the result is valid. This function is typically used in the header of a script or library. It will be demonstrated in the 12th scripts. This script shows a very general shell script Programming Technology: first write a function, and then test it. After that, place the function in a larger and more complete script. This will save you a lot of trouble in the future. Result: The input valid123SAMPLE is valid .. /ValidAlphaNum. sh input: this is most assuredly NOT valid, 12345 input can only be letters and numbers.

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.