I currently have a script that runs upon a user logging into a Linux server and at the end it prints

[Terms of use agreement prints here]

read -p "Agree and continue..." -nl -s

However, most users I find are just "entering" through the prompt (spacebar, enter, random keys, etc) since the script isn't looking for specific entry, and now users have written their own scripts or macros to enter past this.

As a solution, I would like the script to print a string of random numbers, and then require the user to enter those numbers back into the prompt before allowing access.


"Welcome to Server! Please enter the following one-time passphrase to continue: 1234.

Key: "

How can I accomplish this?


It is necessary to use read -p? Otherwise you can do the following (this would be a proof of concept script):

RANDOMNUM=$(shuf -i 0000-9999 -n 1)
echo "Introduce password $RANDOMNUM"
read value
if [ $value == $RANDOMNUM ];
        echo "Password Match!"

Shuf would create a random number of 4 digits between 0000 and 9999.

  • Using read -p is not necessary, it was only being used because I found that solution online previously. I'll give this a shot, thanks! Jan 10 '19 at 20:23
  • Do you know how I could enable the script to execute exit if the numbers are incorrect, or if a non-4 digit numeric entry is provided? Jan 10 '19 at 21:13
  • You have to only add an else to the if as any value that does not match with the condition will be threated as "else". I have modified my previous script to include the else and the exit values.
    – Dasel
    Jan 10 '19 at 21:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.