The shell implementation code for the user input judgment _linux shell

Source: Internet
Author: User

Today's case is the decision to input to the user

#!/bin/sh # validint-validates integer input, allowing negative ints. function Validint {# Validate field. Then Test against Min value $ and/or # max value $ if they are supplied.

 If They are not supplied, skip this tests.   Number= "$";   Min= "$"; Max= "$" if [-Z $number]; Then echo "You didn ' t enter anything. Unacceptable. ">&2; Return 1 fi if ["${number%${number#?}}" = "-"];
Then # is a '-' sign?   Testvalue= "${number#?}" # all but a character else testvalue= "$number" Fi nodigits= "$ (echo $testvalue | sed ' s/[[:d igit:]]//g ')" if [ ! -Z $nodigits]; Then echo "Invalid number format! Only digits, no commas, spaces, etc. ">&2 return 1 fi if [!-Z $min]; Then if ["$number"-lt "$min"]; Then echo "Your value is too small:smallest acceptable value are $min" >&2 return 1 fi fi if [!-Z $ma X]; Then if ["$number"-GT "$max"]; Then echo "Your value is too big:largest accePTable value is $max ">&2 return 1 fi fi return 0} if Validint" $ "" $ "; Then echo "That", is a valid integer value within your constraints "fi

Parsing script:
1) number= "$"; Min= "$"; max= "$" means 3 inputs from the user;
2) nodigits= "$ (echo $testvalue | sed ' s/[[:d igit:]]//g ')" Prepares all numbers for subsequent test user input
3 if Validint "$" "$" "$"; Then note "$" $ "$" to be quoted.
4) The TestValue variable is the number of test inputs to filter for negative numbers.
5 feel very thoughtful.

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.