#!/bin/bash
while true; do
while true; do
read -r -p 'Enter number [4-999]: ' num
if [[ $num =~ ^([4-9]|[1-9][0-9]{1,2})$ ]]; then
break
fi
done
result=$(( 5 * num * (1 + (RANDOM%9)) ))
PS3="Is $result ok? "
select yesno in yes no exit; do
case $REPLY in
1|y) break 2 ;;
2|n) break ;;
3|e) exit ;;
esac
done
done
echo 'Rest of script here'
This script starts with an input loop (the outer while
loop). This is an infinite loop that iterates until the given conditions are satisfied. The conditions for this loop are that the user is happy with some computed value and selects to continue or to terminate the script.
The computed value also requires an input loop (the inner while
loop). This loop iterates until the string the user inputs at the prompt is some number between 4
and 999
.
Once the user has entered a satisfactory string (a whole decimal number in the range [4,999]), the result value is computed and presented to the user along with a selection of options. The input from the user is again performed by an input loop, but in this case, it's a select
loop (since we're presenting the user with a menu of options to pick from).
If the user picks either 1
or y
, the code breaks out of both the select
and the other while
loop using break 2
. If the user picks 2
or n
, the code only breaks out of the select
loop, which causes the program to query again for a number between 4
and 999
. If the user selects 3
or e
, the script terminates via exit
.
tr -cd '0-9'
which outputs digits from 0 to 9 - which is correct?5.7
or9e3
?