Data Validation
Number Validation
Check Digits
Scripts often receive numbers as strings. A validation step should confirm the shape before arithmetic uses the value.
Program
Play the script to choose an input value and see whether the digit check passes.
number_validation.sh
#!/usr/bin/env bash
input=
if [[ "$input" =~ ^[0-9]+$ ]]; then
status="number"
else
status="invalid"
fi
echo "$input:$status"
#!/usr/bin/env bash
input=
if [[ "$input" =~ ^[0-9]+$ ]]; then
status="number"
else
status="invalid"
fi
echo "$input:$status"
validation
Validation checks input before later logic trusts it.
regex
`^[0-9]+$` accepts only one or more digits.
status word
A compact status makes validation failures easy to test.