username, password program in bash

▼魔方 西西 提交于 2020-01-04 03:57:09

问题


I have a program that asks input from user on their username and password then stores it in a text file column one is usernames and column 2 is passwords, i need a command that replaces the password when the user inputs their username and new password, heres what i have

#!/bin/bash
#admin menu

#Register User
echo enter the username of the user you want to register.
read reguser
echo enter the password of the user you want to register.
read regpass

User_Pass="username_pass.txt"

if [ ! -e "$User_Pass" ]; 
then
echo "Creating Username and Passwords file"
touch $User_Pass
fi

echo "$reguser $regpass" | cat >> $User_Pass

echo user succesfully registered.

#Change Password
echo "Enter the username you want to change the password for"
read change1

change2=$(grep -q $change1 username_pass.txt)

if [ $change2=0 ];
then 
echo enter your new password

read newpass



awk -v newpass="$newpass" -v change1="$change1" '$1 ~ change1 {$2 = newpass}' username_pass.txt
#i tried this but it didnt work


    echo "password changed!"
else
    echo "no such username."
fi

回答1:


You can use sed

sed -i "s/$change1.*/$change1 $newpass/" username_pass


来源:https://stackoverflow.com/questions/40442693/username-password-program-in-bash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!