bash 从文件中读取用户名和密码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12400299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading username and password from file
提问by Julian Barrie
I'm looking at a away of reading the username and password of a file and inputing those to either add user or delete user.
我正在考虑读取文件的用户名和密码并输入它们以添加用户或删除用户。
EG: I have a file named 'userlist' with the following content with this format:
EG:我有一个名为“userlist”的文件,其中包含以下格式的内容:
user1 pass1
user2 pass2
user3 pass3
What I don't understand completely is how to use BASH script to add these accounts.
我完全不明白的是如何使用BASH脚本来添加这些帐户。
What I have so far is this:
我到目前为止是这样的:
if [[ whoami -ne "root" ]]
then
exit
else
echo "wish to add or delete? a/d"
read uArg
echo "enter file name"
read uFile
if [ $uArg = "a" -o $uArg = "A" ]
then
IDK WHAT TO DO HERE.
elif [ $uArg = "d" -o $uArg = "D" ]
then
IDK WHAT TO DO HERE.
fi
fi
Alright, what I don't understand is how to read each word line by line and input the username and password to add a new user or delete an existing user.
好吧,我不明白的是如何逐行读取每个单词并输入用户名和密码以添加新用户或删除现有用户。
The program is ment to read the whole file and add each user with there corrosponding password. If delete is chosen then it deletes each user within the file.
该程序将读取整个文件并添加具有相应密码的每个用户。如果选择删除,则会删除文件中的每个用户。
I'm new to BASH so any help would be greatyl appreciated.
我是 BASH 的新手,所以任何帮助都将不胜感激。
回答by Nikita Hismatov
awkperfectly feets your needs.
awk完全满足您的需求。
See this example:
看这个例子:
$ awk '{print "Hi! my name is " ", and my pass is " }' ./userpass.txt
Hi! my name is user1, and my pass is pass1
Hi! my name is user2, and my pass is pass2
Hi! my name is user3, and my pass is pass3
Awk stores usernames in $1 and passwords in $2 (first and second column).
awk 将用户名存储在 $1 中,将密码存储在 $2 中(第一列和第二列)。
You can use pipelines to execute the strings you get from awk as commands:
您可以使用管道将您从 awk 获取的字符串作为命令执行:
$ awk '{print "echo " }' ./userpass.txt | /bin/bash
user1
user2
user3
回答by TaninDirect
something along the lines of...
类似……的东西
if [[ whoami -ne "root" ]]
then
exit
else
echo "wish to add or delete? a/d"
read uArg
echo "enter file name"
read uFile
if [ $uArg = "a" -o $uArg = "A" ]
then
while read user passwd rest
do
if [ ! -z $rest ]; then
echo "Bad data"
else
useradd -m $user
passwd $user <<EOP
$passwd
$passwd
EOP
fi
done < $uFile
elif [ $uArg = "d" -o $uArg = "D" ]
then
while read user passwd rest
do
if [ ! -z $rest ]; then
echo "Bad data"
else
userdel $user
fi
done < $uFile
fi
fi
回答by vvondra
First comments:
第一条评论:
- learn and get used to basic commands like grep, sed, echo, and generally with file manipulation, awk is a good choice as well if you want to know bash basics, a lot you will encounter is about file manipulation
- the code could use more error testing, it's just a basic skeleton
- careful about string and variables, quote them whenever possible, spaces in strings can do a lot of bad
- 学习并习惯基本命令,如 grep、sed、echo,通常还有文件操作,如果您想了解 bash 基础知识,awk 也是一个不错的选择,您会遇到很多关于文件操作的问题
- 代码可以使用更多的错误测试,它只是一个基本的骨架
- 小心字符串和变量,尽可能引用它们,字符串中的空格可以做很多坏事
Could be something along these lines:
可能是这样的:
echo "wish to add or delete? a/d"
read uArg
echo "enter username"
read uName
grep "^$uName " password-file
RET=$?
if [ "$uArg" == "a" -o "$uArg" == "A" ]
then
[ $RET -eq 0 ] && echo "User is already in file"
if [ $RET -ne 0 ]
then
echo "enter password"
read uPass
echo "$uName $uPass" >> password-file
fi
elif [ "$uArg" == "d" -o "$uArg" == "D" ]
then
[ $RET -ne 0 ] && echo "User is not file"
if [ $RET -eq 0 ]
then
sed -i "/^$uName /d" password-file
echo "User deleted"
fi
fi

