1

Scenario. I have this code:

regexp="[^0-9A-Za-z]+"
while [[ $var =~ $regexp ]]
do
{
    var=$(dialog --inputbox "File name (number and letters only): " 12 60 --stdout)
}
done
echo $var

Condition:

The variable var should not be set (is not set) before while statement;

Target:

Leave while statement only when the variable has alphanumeric values.


But if I try to run it don't work. It works if I set up variable var, e.g:

regexp="[^0-9A-Za-z]+"
var="_"
while [[ $var =~ $regexp ]]
do
{
    ...

Now, how to run that code without set up variable var?

1
  • There is an error in your logics here: you must initialize the var before while or place this while into while true; ... done. The regex to match 1 or more alphanumeric characters is [0-9A-Za-z]+. Commented Nov 17, 2015 at 22:19

3 Answers 3

2

You can use default variable value:

regexp="[^0-9A-Za-z]+"
while [[ ${var:-#} =~ $regexp ]]; do
   var=$(dialog --inputbox "File name (number and letters only): " 12 60 --stdout)
done
echo "$var"

${var:-#} will return # of var is null or unset otherwise it returns variable's value.

Sign up to request clarification or add additional context in comments.

Comments

2

Instead of checking if var matches an illegal value, check that is doesn't match a legal value.

# Drop the ^ from the beginning of the character class
regexp="[0-9A-Za-z]+"
# Negate the condition with !
while [[ ! $var =~ ^$regexp$ ]]
do
    var=$(dialog --inputbox "File name (number and letters only): " 12 60 --stdout)
done
echo $var

There is also the little-seen until command (as well as a predefined character class that includes letters and numbers while respecting the current locale):

until [[ $var =~ ^[[:alnum:]]+$ ]]; do

1 Comment

sorry, but don't work. Try yourself using words "test" and "_test". With test get out of while statement, with _test get out of while statement ...but only with test should be out while statement.
2

You can use a while true loop and break if the condition is met:

while true; do
    # Read variable
    var=$(dialog ...)

    # Check regex and break if it matches
    [[ "$var" =~ $regexp ]] && break
done

Btw, the regex should be:

regexp="^[[:alnum:]]+$"

6 Comments

sorry, but if I use this solution I lose exit code of dialog tool.
Sorry, but don't work: endless loop. Try yourself using words test and _test
I did. test is valid meaning it would break the lop. _test is not. Are you using the regex I've posted?
You can save the exit status of dialog with status=$? immediately following the var=... line.
Sorry, endless loop. But it's run properly removing double quote in "Check regex and break if it matches", e.g. with this LOC: [[ $var =~ $regexp ]] && break. Why? Any Idea?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.