How to securely confirm a password by entering it twice in a bash script

I would like to create a bash script where the user can select the username, password and confirm the password by entering it twice. If the passwords do not match the user's request, you must enter it again. If the passwords match, the script should create a password, but otherwise repeat the request until it is correct.

So far I have the code below, but I'm not sure if this is the right way to do this. Is there a problem with the following bash script?

# read username
read -p "Username: " username

# read password twice
read -s -p "Password: " password
echo 
read -s -p "Password (again): " password2

# check if passwords match and if not ask again
while [ "$password" != "$password2" ];
do
    echo 
    echo "Please try again"
    read -s -p "Password: " password
    echo
    read -s -p "Password (again): " password2
done

# create passwordhash
passwordhash=`openssl passwd -1 $password`

# do something with the user and passwordhash
+4
source share
1 answer

Way to reduce detail:

#!/bin/bash

read -p "Username: " username
while true; do
    read -s -p "Password: " password
    echo
    read -s -p "Password (again): " password2
    echo
    [ "$password" = "$password2" ] && break
    echo "Please try again"
done
+7
source

Source: https://habr.com/ru/post/1530588/


All Articles