Building a Password Manager with Shell Scripting
Are you tired of manually managing your passwords in a text file? Do you want to automate the process and make it more secure? In this tutorial, we'll build a simple password manager using shell scripting.
Before we start, make sure that you have a basic understanding of shell scripting and have access to a Linux terminal. We'll be using the Bash shell for this tutorial.
Step 1: Set up the user data file
The first step is to set up the file that will store the user's data. Create a file called user-data.csv in your home directory and give it read and write permissions:
touch ~/user-data.csv
chmod 600 ~/user-data.csv
Step 2: Add a function to add passwords
The next step is to add a function that will allow the user to add passwords to the user-data.csv file. Here's the code for the function:
add_password() {
read -p "Enter account name : " account_id
read -p "Password: " password
echo
echo "$account_id,$password" >> "$user_data_file"
echo
echo "Data Saved"
}
This function prompts the user to enter an account name and password. Once the user has entered the information, it is appended to the user-data.csv file.
Step 3: Add a function to retrieve passwords
The next step is to add a function that will allow the user to retrieve passwords from the user-data.csv file. Here's the code for the function:
get_password() {
read -p "Enter account name : " account_id
if [ -n "$password" ]; then
echo "Your password is : $password"
else
echo "User Id is not found"
fi
}
This function prompts the user to enter an account name. It then uses grep to search the user-data.csv file for lines that start with the entered account name.The awk command is used to extract the password from the matching line.If a password is found, it is displayed to the user. If not, the function displays a message indicating that the account was not found.
Step 4: Add functions to update and delete passwords
To make the password manager more versatile, we can also add functions to update and delete passwords. Here's the code for the update function:
update_password() {
read -p "Enter account id : " account_id
if grep -q "^$account_id," "$user_data_file"; then
read -p "Enter new password : " password
echo
sed -i "s/^\($account_id,\).*/\1$p/" "$user_data_file"
echo "password is updated for $account_id"
else
echo "account Id not found"
fi
}
delete_data() {
read -p "Enter account id : " account_id
if grep -q "^$account_id," "$user_data_file"; then
sed -i "/^$account_id,/d" "$user_data_file"
echo "user details with $account_id is deleted"
else
echo "account not found"
fi
}
This function update_password prompts the user to enter an account name and a new password. It then uses sed to search the user-data.csv file for lines that start with the entered account name and replaces the existing password with the new password. If the account is not found, the function does nothing.
The function delete_data prompts the user to enter an account name. It then uses sed to search the user-data.csv file for lines that start with the entered account name and delete them.
Overall, this script provides a simple and efficient way to manage passwords for multiple accounts. The script is easy to use and does not require any special software or tools. You can simply run the script from the command line and follow the prompts to add, retrieve, update, or delete data.
Here is full code :
#!/bin/bash
# author : Ajay Patel
# date : 18- april -2023
# version : 1.0
#path for file to store data
user_data_file="/home/ubuntu/scripts/user-data.csv"
#function for add user data
add_password() {
read -p "Enter account name : " account_id
read -p "Password: " password
echo
echo "$account_id,$password" >> "$user_data_file"
echo
echo "Data Saved"
}
#function for retrieve data
get_password() {
read -p "Enter account name : " account_id
password=$(grep "^$account_id" $user_data_file | awk -F ',' '{print $2}' )
if [ -n "$password" ]; then
echo "Your password is : $password"
else
echo "User Id is not found"
fi
}
#function for delete data
delete_data() {
read -p "Enter account id : " account_id
if grep -q "^$account_id," "$user_data_file"; then
sed -i "/^$account_id,/d" "$user_data_file"
echo "user details with $account_id is deleted"
else
echo "account not found"
fi
}
#function for update password
update_password() {
read -p "Enter account id : " account_id
if grep -q "^$account_id," "$user_data_file"; then
read -p "Enter new password : " password
echo
sed -i "s/^\($account_id,\).*/\1$p/" "$user_data_file"
echo "password is updated for $account_id"
else
echo "account Id not found"
fi
}
#to get input from user
read -p "Enter action (add/get/update/delete):" action
#check user action
if [ "$action" == "add" ]; then
add_password
elif [ "$action" == "get" ]; then
get_password
elif [ "$action" == "delete" ]; then
delete_data
elif [ "$action" == "update" ]; then
update_password
else
echo "invalid selection"
fi
I hope this blog post has been helpful in understanding how to manage passwords using a Bash script. If you have any questions or feedback, please feel free to leave a comment below.
Happy coding!
#bashscripting #passwordmanager #linux #automation #security #scripting #csv #commandline #devops



