dartokloning ревизий этого фрагмента . К ревизии
1 file changed, 77 insertions
PasswordValidation.sh(файл создан)
| @@ -0,0 +1,77 @@ | |||
| 1 | + | #!/bin/bash | |
| 2 | + | ||
| 3 | + | #Validation Function: | |
| 4 | + | #- Checks if the password is not empty. | |
| 5 | + | #- Ensures the password length is between 5 and 16 characters. | |
| 6 | + | #- Ensures the password is not the same as the username. | |
| 7 | + | #- Checks if the password contains no spaces. | |
| 8 | + | #- Ensures the password contains at least one uppercase letter. | |
| 9 | + | #- Ensures the password contains at least one number. | |
| 10 | + | #- Ensures the password contains at least one special character. | |
| 11 | + | ||
| 12 | + | #Input and Validation: | |
| 13 | + | #- Prompts the user to enter a username and password. | |
| 14 | + | #- Calls the validation function to check the entered password. | |
| 15 | + | #- Displays appropriate messages based on the validation result. | |
| 16 | + | ||
| 17 | + | # Function to validate the password | |
| 18 | + | function validate_password() { | |
| 19 | + | local password="$1" | |
| 20 | + | local username="$2" | |
| 21 | + | ||
| 22 | + | # Check if password is not empty | |
| 23 | + | if [[ -z "$password" ]]; then | |
| 24 | + | echo "Password cannot be empty." | |
| 25 | + | return 1 | |
| 26 | + | fi | |
| 27 | + | ||
| 28 | + | # Check if password length is between 5 and 16 characters | |
| 29 | + | if [[ ${#password} -lt 5 ]] || [[ ${#password} -gt 16 ]]; then | |
| 30 | + | echo "Password must be between 5 and 16 characters long." | |
| 31 | + | return 1 | |
| 32 | + | fi | |
| 33 | + | ||
| 34 | + | # Check if password is the same as the username | |
| 35 | + | if [[ "$password" == "$username" ]]; then | |
| 36 | + | echo "Password cannot be the same as the username." | |
| 37 | + | return 1 | |
| 38 | + | fi | |
| 39 | + | ||
| 40 | + | # Check if password contains spaces | |
| 41 | + | if [[ "$password" =~ \ ]]; then | |
| 42 | + | echo "Password cannot contain spaces." | |
| 43 | + | return 1 | |
| 44 | + | fi | |
| 45 | + | ||
| 46 | + | # Check if password contains at least one uppercase letter | |
| 47 | + | if ! [[ "$password" =~ [A-Z] ]]; then | |
| 48 | + | echo "Password must contain at least one uppercase letter." | |
| 49 | + | return 1 | |
| 50 | + | fi | |
| 51 | + | ||
| 52 | + | # Check if password contains at least one number | |
| 53 | + | if ! [[ "$password" =~ [0-9] ]]; then | |
| 54 | + | echo "Password must contain at least one number." | |
| 55 | + | return 1 | |
| 56 | + | fi | |
| 57 | + | ||
| 58 | + | # Check if password contains at least one special character | |
| 59 | + | if ! [[ "$password" =~ [^a-zA-Z0-9] ]]; then | |
| 60 | + | echo "Password must contain at least one special character." | |
| 61 | + | return 1 | |
| 62 | + | fi | |
| 63 | + | ||
| 64 | + | return 0 | |
| 65 | + | } | |
| 66 | + | ||
| 67 | + | # Get username and password input from the user | |
| 68 | + | read -p "Enter username: " username | |
| 69 | + | read -s -p "Enter password: " password | |
| 70 | + | echo | |
| 71 | + | ||
| 72 | + | # Validate the password | |
| 73 | + | if validate_password "$password" "$username"; then | |
| 74 | + | echo "Password is valid." | |
| 75 | + | else | |
| 76 | + | echo "Password is invalid. Please try again." | |
| 77 | + | fi | |
Новее
Позже