Bash: 在 .txt 文件中搜索和替换密码
Bash: Search and replace password in a .txt file
我想弄清楚如何在 .txt 文件的特定行和特定列上搜索和替换密码。这是它的样子:
Admin1 Pass1 1
Admin2 Pass2 1
User1 Upass1 0
User2 Upass2 0
这是我的代码:
while (true)
do
read -p 'Whose password would you like to change? Enter the corresponding user name.' readUser
userCheck=$(grep $readUser users.txt)
if [ "$userCheck" ]
then
echo $userCheck > temp2.txt
read -p 'Enter the old password' oldPass
passCheck=$(awk '{print}' temp2.txt)
if [ "$passCheck" == "$oldPass" ]
then
read -p 'Enter the new password' newPass
sed -i "/^$readUser/ s/$oldPass/$newPass/" users.txt
break
else
echo 'The username and/or password do not match. Please try again.'
fi
else
echo 'The username and/or password do not match. Please try again.'
fi
done
假设 User1 的密码被替换为 TESTING,结果如下:
Admin1 Pass1 1 Admin2 Pass2 1 User1 TESTING 0 User2 Upass2 0
我需要的是:
Admin1 Pass1 1
Admin2 Pass2 1
User1 TESTING 0
User2 Upass2 0
你的原稿已经差不多完成了,只是缺少正确的引用。你可以用双引号写 : echo "$updatePass" > data
来保留换行符。有关报价的更多信息 here
不过,您的脚本还有改进的余地。你可以这样写:
#!/bin/bash
while (true)
do
read -p 'Whose password would you like to change?' readUser
# no need for a temporary variable here
if [ "$(awk -v a="$readUser" '==a{print }' users.txt)" ]
then
read -p 'Enter the old password' oldPass
# the awk code checks if the $oldPass matches the recorded password
if [ "$oldPass" == "$(awk -v a="$readUser" '==a{print }' users.txt)" ]
then
read -p 'Enter the new password' newPass
# the -i flag for sed allows in-place substitution
# we look for the line begining by $readUser, in case several users have the same password
sed -i "/^$readUser/ s/$oldPass/$newPass/" users.txt
break
else
echo 'The username and/or password do not match. Please try again.'
fi
else
echo 'The username and/or password do not match. Please try again.'
fi
done
我想弄清楚如何在 .txt 文件的特定行和特定列上搜索和替换密码。这是它的样子:
Admin1 Pass1 1
Admin2 Pass2 1
User1 Upass1 0
User2 Upass2 0
这是我的代码:
while (true)
do
read -p 'Whose password would you like to change? Enter the corresponding user name.' readUser
userCheck=$(grep $readUser users.txt)
if [ "$userCheck" ]
then
echo $userCheck > temp2.txt
read -p 'Enter the old password' oldPass
passCheck=$(awk '{print}' temp2.txt)
if [ "$passCheck" == "$oldPass" ]
then
read -p 'Enter the new password' newPass
sed -i "/^$readUser/ s/$oldPass/$newPass/" users.txt
break
else
echo 'The username and/or password do not match. Please try again.'
fi
else
echo 'The username and/or password do not match. Please try again.'
fi
done
假设 User1 的密码被替换为 TESTING,结果如下:
Admin1 Pass1 1 Admin2 Pass2 1 User1 TESTING 0 User2 Upass2 0
我需要的是:
Admin1 Pass1 1
Admin2 Pass2 1
User1 TESTING 0
User2 Upass2 0
你的原稿已经差不多完成了,只是缺少正确的引用。你可以用双引号写 : echo "$updatePass" > data
来保留换行符。有关报价的更多信息 here
不过,您的脚本还有改进的余地。你可以这样写:
#!/bin/bash
while (true)
do
read -p 'Whose password would you like to change?' readUser
# no need for a temporary variable here
if [ "$(awk -v a="$readUser" '==a{print }' users.txt)" ]
then
read -p 'Enter the old password' oldPass
# the awk code checks if the $oldPass matches the recorded password
if [ "$oldPass" == "$(awk -v a="$readUser" '==a{print }' users.txt)" ]
then
read -p 'Enter the new password' newPass
# the -i flag for sed allows in-place substitution
# we look for the line begining by $readUser, in case several users have the same password
sed -i "/^$readUser/ s/$oldPass/$newPass/" users.txt
break
else
echo 'The username and/or password do not match. Please try again.'
fi
else
echo 'The username and/or password do not match. Please try again.'
fi
done