Basically looking for a string that contains an asterisk, looked all around for one that relates to an until loop but to no avail!
e.g. the user would basically keep typing in strings and the program will reject them until one that has an asterisk pops up.
then at this point it will echo a message [cant go into details sorry]
code:
until [ "string" == "[asteriskstuff]" ]
do
echo "just give me a string with an asterisk for crying out loud"
read "string"
done
when the user stops inputting hi and then decides to input hi* it will then show the echo message basically I just want to bypass the bleedin' asterisk because [asteriskstuff] is the area where I always falter!
all help appreciated!
SUMMARY: How can I bypass an asterisk in a simple way for an until loop?
chepner's helpful answer explains the problem with your original approach well.
The following prompts until a literal *
is found anywhere in the user input:
until [[ $string == *'*'* ]]; do
echo "just give me a string with an asterisk, for crying out loud"
read -r string
done
[[ ... ]]
rather than [ ... ]
must be used in order to enable pattern matching on the RHS of ==
(=
).
/bin/sh
), where only [ ... ]
is supported, consider using [[ ... ]]
routinely, because it offers more features and fewer surprises: see this answer of mine.*'*'*
is a pattern that says: match a literal *
- the single-quoted part - anywhere in the string; the unquoted *
instances represent any run of characters.
[[ ... =~ ... ]]
. Thus, the conditional could also be written as [[ $string =~ '*' ]]
or [[ $string =~ \* ]]
-r
should be used with read
as a matter of habit, to prevent unexpected processing of \
chars.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With