omkar sohani
omkar sohani

Reputation: 147

shell scripting and regular expression

#!bin/bash
echo enter your password :
read password

passlength=$(echo ${#password})

if [ $passlength -le 8 ];
then
    echo you entered correct  password
else
    echo entered password is incorrect
fi

if [[$password == [a-z]*[0-9][a-z]*]];
then
    echo match found
else
    echo match not found
fi

I am not getting what's wrong with this code. If I enter any string as a password, let's say hello123, it gives me an error:

hello123 : command not found

What is wrong with my script?

Upvotes: 1

Views: 22966

Answers (6)

jaypal singh
jaypal singh

Reputation: 77185

You can do the following to make it work cross-platforms with any the bourne shell (/bin/sh) based shell, no bash specific primitives -

echo "$password" | grep -q "[a-z]*[0-9][a-z]*"
if [ $? -eq 0 ] ;then
    echo "match found"
else
    echo "match not found"
fi

Also feel free to use quotes around the variable names. It will save you hours and hours worth of useless debugging. :)

Upvotes: 9

arulraj.net
arulraj.net

Reputation: 4837

#!/bin/bash
read -s -p "Enter Password: " password
password_length=${#password}
if [ $password_length -lt 8 -o $password_length -gt 20 ] ;then 
        echo -e "Invalid password - should be between 8 and 20 characters in length.";
        echo ;
    else
        # Check for invalid characters
        case $password in 
            *[^a-zA-Z0-9]* ) 
                echo -e "Password contains invalid characters.";
                echo ;
                ;;  
            * )
                echo "Password accepted.";
                echo ;
                break;
                ;;
        esac
fi

More tuned example..

Upvotes: 0

gsbabil
gsbabil

Reputation: 7703

The corrected script is below. The errors were:

  • #!/bin/bash, not #!bin/bash
  • To read password length, just do passlength=${#password}, not passlength=$(echo ${#password})
  • Always put a space after [ or [[

#!/bin/bash
echo "enter your password :"
read password

passlength=${#password}

if [[ $passlength -le 8 ]]
then
    echo "you entered correct password"
else
    echo "entered password is incorrect"
fi

if [[ $password == [a-z]*[0-9][a-z]* ]]
then
    echo "match found"
else
    echo "match not found"
fi

Upvotes: 1

glenn jackman
glenn jackman

Reputation: 247220

In the bash [[ construct, the == operator will match glob-style patterns, and =~ will match regular expressions. See the documentation.

Upvotes: 0

bitmask
bitmask

Reputation: 34714

Technically it should give you an error like [[hello123 : command not found.

The issue is that [[$password is not expanded how you think it is. Bash will first resolve the $password variable to what you entered (i.e. hello123). This will yield the string [[hello123 which bash will then try to invoke (and fail, as there is nothing with that name).

Simply add a space () after [[ and bash will recognise [[ as the command to run (although it is a builtin).

if [[ "$password" == [a-z]*[0-9][a-z]* ]]
then
  ...

Upvotes: 2

Slava Semushin
Slava Semushin

Reputation: 15214

Try to replace line

if [[$password == [a-z]*[0-9][a-z]*]];

with following

if echo "$password" | grep -qs '[a-z]*[0-9][a-z]*'

HTH

Upvotes: -1

Related Questions