-1

I have the assignment to write a bash script to validate password strength and one of the requirements is: Include both the small and capital case letters. The Password variable is the input, of course, one of the requirements to validate the password is that the password has to contain upper case letters, lower case letters, and numbers. It would be enough even if only one letter is upper case and one is the lower case but for that don't I need to go over the string with for loop and compare each letter to upper case and lower case? Only one case of both of them would be enough.

if  [[ $PASSWORD == ${PASSWORD,,} ]] &&
    [[ $PASSWORD == ${PASSWORD^^} ]]; then
        valid=true
else
        valid=false
fi

It always will give me false.

Can anyone please help me, thank you!

  • 1
    you're asking if `PASSWORD` is equal to itself when converted to all lowercase ***and*** when converted to all uppercase ... hard to do if there's a letter in `PASSWORD`, could get 'true' if `PASSWORD` is a bunch of numbers and/or non-alphabetic characters; you've stated this is an assignment so I'm guessing ... homework? and if so, what have you learned recently? anything having to do with `=~` or wildcard matching? – markp-fuso Nov 09 '21 at 14:03
  • 1
    Change `&&` to `||` and switch true and false. If it matches either one, the password contains only upper case, or only lower case, or none at all (such as `1234`), and hence doesn't contain both lower and upper case letters. – dan Nov 09 '21 at 14:10

1 Answers1

1

Your test is always false except if "PASSWORD" do not contains letters!

#! /bin/bash

...

if  [[ "$PASSWORD" =~ [a-z] ]] && [[ "$PASSWORD" =~ [A-Z] ]]; then
        valid=true
else
        valid=false
fi
Arnaud Valmary
  • 2,039
  • 9
  • 14