Allows space before and after the number, _33, or 33___ , but not 3__3. and no letters.
0 or
-- Get input from user until Correct
# -- get input until Correct
unset get_num
while [[ ! ${get_num} =~ ^[0-9]+$ ]]; do
echo "Please enter in a number:"
read get_num
done
echo This is a number : ${get_num}
-- Get input until Correct (within range)
# -- get input until Correct (within range)
unset get_num
while [[ ! ${get_num} =~ ^[0-9]+$ ]]; do
echo "Please enter in a number within range of (1-30):"
read get_num
! [[ ${get_num} -ge 1 && ${get_num} -le 30 ]] && unset get_num
done
echo This is a number withn a range : ${get_num}
-- get input until Correct (within range) (full regex way)
"Since regular expressions deal with text rather than with numbers, matching a number in a given range takes a little extra care. You can't just write [0-255] to match a number between 0 and 255. Though a valid regex, it matches something entirely different. [0-255] is a character class with three elements: the character range 0-2, the character 5 and the character 5 (again). This character class matches a single digit 0, 1, 2 or 5, just like [0125]." ~ http://www.regular-expressions.info/numericranges.html
# -- get input until Correct (within range) (another way)
unset get_num
while [[ ! ${get_num} =~ ^([1-9]|1[0-9]|2[0-9]|30)$ ]]; do
echo "Please enter in a number within range of (1-30):"
read get_num
done
echo This is a number withn a range : ${get_num}
-- Get input, and check it only (no while loop)
# -- get input, and check it only (no while loop)
unset get_num
echo "Please enter in a number:"
read get_num
if [[ ! ${get_num} =~ ^[0-9]+$ ]] ;then
echo "${get_num} isn't a number"
else
echo "${get_num} is a number"
fi