[英]username and password checking from another file using shell script
I'm new to shell, I want to do the following task 我是Shell的新手,我想执行以下任务
A shell program which accepts username and password and checks the $username and $password with a line in another file (for authentication), I know how to get the username from user but how do I check against a line in another file. 一个shell程序,它接受用户名和密码,并用另一个文件中的一行(用于身份验证)检查$ username和$ password,我知道如何从用户那里获取用户名,但是如何检查另一个文件中的一行。 say I have a file called username (which has the username) and file called password (which has the password) how do I check $username with that line and $password with that line in the file
说我有一个名为username的文件(具有用户名)和名为password的文件(具有密码),如何检查文件中该行的$ username和该行的$ password
Straight from the perl cookbook: (I guess it's no good though because the OP wanted bash) 直接来自perl食谱:(我猜这不好,因为OP想要bash)
#!/usr/bin/perl -w
#
checkuser - demonstrates reading and checking a user's password
use Term::ReadKey;
print "Enter your password: ";
ReadMode 'noecho';
$password = ReadLine 0;
chomp $password;
ReadMode 'normal';
print "\n";
($username, $encrypted) = ( getpwuid $< )[0,1];
if (crypt($password, $encrypted) ne $encrypted) {
die "You are not $username\n";
} else {
print "Welcome, $username\n";
}
Super basic and has no security but this should work for finding them in a file 超级基础,没有安全性,但是应该可以在文件中找到它们
How i think it should be done(single file) 我认为应该怎么做(单个文件)
echo 'Type username'
read username
echo 'Type Password'
read -s Password
awk /^$username.*$Password$/' {print "found"}' loginfile
For the way OP has files set up 对于OP设置文件的方式
echo 'Type username'
read username
echo 'Type Password'
read -s Password
UserCheck=$(awk /^$username$/' {print "1"}' Username.txt)
PassCheck=$(awk /^$Password$/' {print "1"}' Password.txt)
if [[ UserCheck -eq 1 && PassCheck -eq 1 ]]; then
echo "Found"
else
echo "not found"
fi
Here is an example script which prompts the user to enter a username and password and then compares them against the username and password taken from two files: 这是一个示例脚本,提示用户输入用户名和密码,然后将它们与从两个文件中获取的用户名和密码进行比较:
#!/bin/bash
# prompt the user to enter a username and password
read -p "Username: " inputUsername
read -s -p "Password: " inputPassword
# read the username and password from file
username=$(<username.txt)
password=$(<password.txt)
# compare
if [[ "$inputUsername" == "$username" && "$inputPassword" == "$password" ]]
then
echo "valid username and password"
else
echo "invalid username and password"
fi
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.