Fun with Bash Double Brackets, Regular Expressions, Case Matching, and Digits

After some quick searching and not finding the answer, I decided to write this up for my own reference.

My original inquiry was how do I form a double-bracketed if branch statement, using "=~" to check a variable against a regular expression for upper OR lower case of a specific search string in bash. After some working it out, I think I got it. See below.

For instance, in the example script below, the user is asked to answer yes or no, the value entered is then checked to "loosely" match a predefined value. In this case, yes/y (with any combination of case) will match the Regular Expression.

This example points out how to formulate your bracketed regular expression to match any variations in case (or even a single character answer, e.g., y OR n). There are differences with the bracketed use of regular expressions compared to how grep uses them, I'm finding.

Like, notice in the experimental script below, how single quotes are not used in the bracketed expression, and the "|" for the OR situation is not escaped with \. Also, with the example that checks for exactly 6 characters, no "\" are used to escape the curly braces.


#!/bin/bash

while [ 1 ]
do
    echo "Are you ready to get started? Enter: Yes|No"
    read result

    #Test the result for yes/no (or variation)
    if [[ "$result" =~ ^[Yy][Ee][Ss]$|^[Yy]$ ]]
    then
        echo "Good. Grab your stuff and let's roll!"
        break
    elif [[ "$result" =~ ^[Nn][Oo]$|^[Nn]$ ]]
    then
        echo "You answered No. Ok, we'll wait until later."
        break
    elif [[ "$result" =~ ^[Ee][Gg][Gg]$ ]]
    then
        echo "Congrats! You found the Easter Egg!"
        while [ 1 ]
        do
            echo "Enter a 6 digit number:";
            read theresult
            if [[ "$theresult" =~ ^[0-9]{6}$ ]]
            then
                echo "Nice work! Bye!"
                break
            else
                echo "Input not recognized, please enter 6 digits."
            fi
        done
        break
    else
        echo "Input not recognized. Please enter Yes/No."
    fi
done

exit 0
 
 

So there it is, just a quick example of how bracketed regular expressions can be used to test for specific values in bash. Hopefully this little example will help somebody save a bit of time having to research for this information.



Cheers!


Shannon VanWagner


07-17-2012



Comments

  1. Hi, I was looking for something like this and got to your page from Google. I copied your code and ran it from my command line and it did not work. Neither Yes, Y, No or N worked for me.

    ReplyDelete
  2. @Steve, rather than being run from the command line..this code is formatted to be put in a file, then the file should be set to be executable with chmod +x filename , then run with ./filename

    ReplyDelete
  3. slightly cleaned up version of yours. could cut the amount of code used but it is structured nicely

    #!/bin/bash

    while true; do
    clear
    echo -n "Are you ready to get started? Enter: Yes|no: "
    read result

    #Test the result for yes/no (or variation)
    if [[ "$result" =~ ^[Yy][Ee][Ss]$|^[Yy]$ ]]
    then
    echo "Good. Grab your stuff and let's roll!"
    break
    elif [[ "$result" =~ ^[Nn][Oo]$|^[Nn]$ ]]
    then
    echo "You answered No. Ok, we'll wait until later."
    break
    elif [[ "$result" =~ ^[Ee][Gg][Gg]$ ]]
    then
    echo "Congrats! You found the Easter Egg!"
    while true; do
    echo -n "Enter a 6 digit number: "
    read theresult
    if [[ "$theresult" =~ ^[0-9]{6}$ ]]
    then
    echo "Nice work! Bye!"
    break
    else
    echo "Input not recognized, please enter 6 digits."
    fi
    done
    break
    else
    echo "Input not recognized. Please enter Yes/no."
    sleep 2s
    fi
    done

    exit 0

    ReplyDelete

Post a Comment

Thanks for commenting. Comments are moderated by the blog owner and will appear once approved. Need to email me directly? Go to http://shannonvanwagner.com/email-me.php

Popular posts from this blog

On Helping Others Get their GNU/Linux & Consider Doing So

How To: Install Wordpress on Manjaro Linux Pahvo 21.1.0

How to Disable Middle Mouse Button in Ubuntu / Debian Linux