Find the answer to your Linux question:
Results 1 to 4 of 4
I'm using Ubuntu 10.04 LTS and I need to write a bash shell function to validate that a password (given as an argument to the function call) has: - at ...
  1. #1
    Just Joined!
    Join Date
    May 2010
    Posts
    4

    Validate password (BASH script) - regex problem

    I'm using Ubuntu 10.04 LTS and I need to write a bash shell function to validate that a password (given as an argument to the function call) has:

    - at least 2 numbers
    - at least 2 of these characters: #$%
    - is at least 8 chars long

    I tried this regex and tested it online - it worked. But not in bash!

    Code:
    ^(?=(.*\d){2,})(?=(.*[#$%]){2,}).{8,}$
    It seems to me that the positive lookahead (?=blablabla) doesn't work at all in bash, but I can't find any info on it...

    I tried this simple regex for the password "hello":

    Code:
    ^h(?=e).*$
    And it didn't work!

    Here's my full script:

    Code:
    #!/bin/bash
    function test_password {
      if [[ "$1" =~ ^h(?=e).*$ ]]
      then
        echo "Password OK."
      else
        echo "Bad password!"
      fi
    }
    test_password 'hello'
    --> It echoes "Bad password"

    I also tried it with egrep, still nothing.
    Does bash support positive lookaheads? If yes, what am I doing wrong?
    If not with regular expressions, how could I test if a string has 2 digits, etc?
    Thanks!!!

  2. #2
    Just Joined!
    Join Date
    May 2010
    Posts
    5
    i don't know about bash scripting but wouldn't be it easier to make three variables (numbers, specialchars, charcount) and iterate through every character of the password and increase the variable numbers/specialchars if a number/specialchar is found? and after all you could look if all requirements for a safe password are fullfilled.

    just an idea.

  3. #3
    Linux User
    Join Date
    Nov 2009
    Location
    France
    Posts
    292
    The return code after the comparison indicates that the regex is incorrect. Complex regex are quite obscure to me, I won't be able to define your desired regex.

    Referencing bash man pages :
    An additional binary operator, =~, is available, with the same precedence as == and !=. When it is used, the string to the right of the operator is considered an extended regular expression and matched accordingly (as in regex(3)). The return value is 0 if the string matches the pattern, and 1 otherwise. If the regular expression is syntactically incorrect, the conditional expression's return value is 2.
    You may have greater flexibility if you verify independantly each condition, by whatever technique, as already suggested by another forum member. (Side note : Simple ones are always preferable in my view, even if they are not dazzling.)
    0 + 1 = 1 != 2 <> 3 != 4 ...
    Until the camel can pass though the eye of the needle.

  4. #4
    Just Joined!
    Join Date
    May 2010
    Posts
    4
    That's what I did in the end (I iterated through each character), yes... Thanks!

    However, I'm still interested in how good the support for regular expressions is in bash, if anyone knows... I tried extended regexes with the =~ operator, I tried with egrep... Simpler expressions worked, but the lookahead (?=pattern) did not.
    That's it from me for now... Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...