I am having to modify a bash shell script, but I don't have much experience with bash shell scripting (I have worked with many other languages, but just not much with shell scripting).
The script displays a menu with "X" items, and then it prompts the user to enter a number.
After the user enters a number, I think that the script checks the number/string that was entered to make sure it is a digit (I think) and then also if the number is within a range of values.
Part of what I want to change is the number of items in the menu and there were originally 9 items, but I need to add another 10 or so. It looks like the check is failing if the number entered is more than 1 digit.
Here's the snippet where it is doing the check:
while menu && read -rp "$prompt" num && [[ "$num" ]]; do
if [[ "$num" == [[:digit:]] ]] && [[ $num -gt 0 ]] && [[ $num -le ${#service_accounts[@]} ]]; then
I think the part of the "if" that is failing is:
[[ "$num" == [[:digit:]] ]]
Can someone tell me if that comparison would fail if the string in $num is more than 1 digit?
And, also, if that is the case, how can I change that comparison to work correctly even if the $num has more than 1 digit?
Sorry if this is a kind of odd type of question :(!! And, thanks in advance!
Jim
[[ "$num" == [[:digit:]] ]]
fails with everything but one digit.
This enables your script to accept numbers with one to two digits:
[[ "$num" =~ ^[[:digit:]]{1,2}$ ]]
=~
compares $num with regex ^[[:digit:]]{1,2}$
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.