Dargenn
Dargenn

Reputation: 402

Validate user password according to regex bash

I've been trying to write bash script, that validates user input with given rules: length > 8, at least one digit, and at least one of these: [@, #, $]

So regex for that is this:

((?=.*\d)(?=.*[@#$%&*+-=]).{8,})

I've tried this, but with no result:

result=$(echo $1 | egrep "((?=.*\d)(?=.*[@#$%&*+-=]).{8,})")
echo $result

with $1 being input parameter. Also, I'd like to wrap it in IF clause, but echo never outputs anything. What am i doing wrong?

Upvotes: 1

Views: 324

Answers (1)

Cyrus
Cyrus

Reputation: 88731

This might help:

[[ ${#1} -ge 8 && $1 =~ [0-9] && $1 =~ [@#$] ]] && result="$1"

or with three grep:

result=$(grep -E '.{8}' <<< "$1" | grep '[0-9]' | grep '[@#$]')

Upvotes: 3

Related Questions