0

Hi Im not any sort of programmer but found myself creating an install script which installs mySQL which works fine. I would like to improve the script by asking the user to enter text that will be used for a password.

My understanding is that a reasonable way of doing this would be to use the 'echo' and 'read' commands to obtain their input and then I would like to check that only standard alpha numeric characters are used and if not ask the user to try again. I have not been able to understanding grep or piping so am asking if anyone would be willing to write it in a way that I might just drop it into a bash script.

Hoping ns

1 Answer 1

2

You can try the following bash function

#!/bin/bash

function get_password(){
    echo "Enter wour password: "
    read password
    if [[ -n "$password" ]]; then
        a=$(echo $password | tr -d "\n" | wc -c)
        b=$(echo $password | tr -cd "[:alnum:]" | wc -c)

        if [[ $a != $b ]]; then
            echo "Plz enter alphanumeric words"
            echo "Retry or Press Ctrl + C to exit"
            get_password
        else
            echo "it is fine"
            exit 0
        fi
    else
        echo "password can't be left empty"
        echo "Retry or Press Ctrl + C to exit"
        get_password
    fi
}

get_password

How it works:

./script.sh 
Enter wour password: 
12%
Plz enter alphanumeric words
Retry or Press Ctrl + C to exit
Enter wour password: 
^C

./script.sh 
Enter wour password: 
123sddf
it is fine

See man tr and man wc to know more about tr and wc.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .