我是shell新手,我想做以下任务
一个shell程序,它接受用户名和密码并用另一个文件中的一行检查$ username和$ password(用于身份验证),我知道如何从用户那里获取用户名,但我如何检查另一个文件中的一行。说我有一个名为username(有用户名)的文件和名为password(有密码)的文件如何用该行检查$ username和文件中该行的$ password
答案 0 :(得分:0)
直接来自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";
}
答案 1 :(得分:0)
超级基本且没有安全性,但这应该可以在文件中找到它们
我认为应该怎么做(单个文件)
echo 'Type username'
read username
echo 'Type Password'
read -s Password
awk /^$username.*$Password$/' {print "found"}' loginfile
对于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
答案 2 :(得分:0)
这是一个示例脚本,提示用户输入用户名和密码,然后将它们与从两个文件中获取的用户名和密码进行比较:
#!/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