101 scripts of reading notes No. 4

Source: Internet
Author: User
Today's case is TheCode #! /Bin/sh # validint -- Validatesintegerinput, allowingnegativeintstoo. functionvalidint {# Validatefirstfield. Thentestagainst...

 

 

In today's case, we will judge user input.

The Code

#! /Bin/sh

# Validint -- Validates integer input, allowing negative ints too.

 

Function validint

{

# Validate first field. Then test against min value $2 and/or

# Max value $3 if they are supplied. If they are not supplied, skip these tests.

 

Number = "$1"; min = "$2"; max = "$3"

 

If [-z $ number]; then

Echo "You didn't enter anything. Unacceptable."> & 2; return 1

Fi

 

If ["$ {number % $ {number #?}} "="-"]; Then # is first char A'-'Sign?

Testvalue = "$ {number #?} "# All but first character

Else

Testvalue = "$ number"

Fi

 

Nodigits = "$ (echo $ testvalue | sed's/[[: digit:] // 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 is $ min"> & 2

Return 1

Fi

Fi

If [! -Z $ max]; 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 "$1" "$2" "$3"; then

Echo "That input is a valid integer value within your constraints"

Fi

 

 

 

Parsing script:

1) number = "$1"; min = "$2"; max = "$3" indicates three inputs of the user;

2) nodigits = "$ (echo $ testvalue | sed's/[[: digit:] // g')" to test whether all input by the user is a number.

3) if validint "$1" "$2" "$3"; then note that quotation marks are required for "$1" "$2" "$3.

4) The testvalue variable is used to filter negative numbers and test whether all input values are numbers.

5) I felt like I was thinking well.

 

 

 

 

This article is from the blog "you just want to boast"

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.