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"