How To: Check if Username and Password are Valid using Bash Script

6744

It’s time to create a bash script that will take inputs – Username and Password, from the user and validate if the username-password combination is correct. There will be two essential steps involved in this script- check if username is valid and in case username is valid, check if password associated with that username is valid.

Let’s start scripting!

1. Check if username is valid

This, surely, is the easier part of the script. Validating a user is very straight-forward, as you can use id command with option -u or read /etc/passwd (using ‘grep‘ command) file to do so (Read about /etc/passwd file format here). If the exit status is “0”, then it’s a valid user, else it’s not. For those who are new to bash scripting- Exit status can be checked using echo $?.
 
Read more on YourOwnLinux