一、说明
Linux中用shell脚本实现用户注册,登录,删除特点:用户和密码是单另储存在一个文件中,程序结束后,下次登录只要存储用denglu.sh 主目录
denglu1.sh 注册功能模块
denglu2.sh 登录功能模块
denglu3.sh 删除单个用户功能模块
name_passwd 储存用户密码文件
name_save 储存用户名文件户的两个文件不删除,可直接登录
二、代码
1、联合
#联合
echo 请问您之前有注册过本程序的用户么?
echo 若有请输入Y 若没有请输入N
echo 若您想直接退出本程序请随意输入任意一个字符
read choiceif test $choice = Y
then./denglu2.sh
elif test $choice = N
then./denglu1.sh
fi
2、注册
#注册
name=('')
passwd=('')echo 请注意一旦新注册用户
echo 之前已注册用会会全部删除!!!!
echo 若想继续请输入Y 退出注册程序请输入N
read choice
if test $choice = N
thenbreak
fi
echo "" > name_save
echo "" > name_passwd
echo 请问您想注册几个用户
read numfor (( i = 0 ; $i < $num ; i=$i+1))
do let n=$i+1echo 请输入第 $n 个用户的用户名read namname[$i]=$namecho ${name[$i]} >> name_saveecho 请输入第 $n 个用户的密码read nampasswd[$i]=$nam echo ${passwd[$i]} >> name_passwd
done
echo 注册完成请重新登录./denglu2.sh
3、登录
#登录
name=('')
mapfile -t -s 1 name < name_save passwd=('')
mapfile -t -s 1 passwd < name_passwdecho 请输入您的用户名
read nam
for (( i=0 ; $i < ${#name[*]} ; i=$i+1 ))
do n=3 #密码输入次数计数if test $nam = ${name[$i]} thenfor (( ; n != -1 ; ))doecho 请输入密码read miif test $mi = ${passwd[$i]}thenecho 已成功登录 ${name[$i]} 用户let z=1breakelseecho 密码输入错误echo 您还有 $n 次输入机会let n=$n-1fidoneif test $n = -1thenlet z=0echo 请问您是否需要返回主目录echo 需要请输入Y 不需要请输入Necho 如果需要推出本程序 请随意输入一个字符read choiceif test $choice = Ythen./denglu.shelif test $choice = Nthenecho 请重新登录./denglu2.shelsebreakfifibreakfi
doneif test $i = ${#name[*]} thenecho 请问您是否需要返回主目录echo 需要请输入Y 不需要请输入Necho 如果需要推出本程序 请随意输入一个字符read choiceif test $choice = Ythen./denglu.shelif test $choice = Nthenecho 请重新登录./denglu2.shfifi
if test $z = 1
thenecho 请问您需要删除某个用户么?echo 是请输入Y 否请输入N echo 若需要推出登录返回主页面请随意输入一个字符read choiceif test $choice = Ythen./denglu3.shelif test $choice != N then ./denglu.sh#else#留下的接口和增添功能使用fi
fi
4、用户删除
#用户删除
name=('')
mapfile -t -s 1 name < name_savepasswd=('')
mapfile -t -s 1 passwd < name_passwdnum=${#name[*]}echo 目前有以下这 $num 个用户
for ((i = 0 ; i < $num ; i=$i+1 ))
do let n=$i+1echo 这是第 $n 个用户echo ${name[i]}
doneecho 请问您想删除第几个用户
read man echo "" > name_save
echo "" > name_passwdname1=('')
passwd1=('')u=0
let man=$man-1for (( i = 0 ; i < $num ; i=$i+1 ))
do if test $i != $manthenname1[u]=${name[$i]}echo "${name1[$u]}" >> name_savepasswd1[u]=${passwd[$i]}echo "${passwd1[$u]}" >> name_passwdlet u=$u+1elsecontinuefi
doneecho 目前的用户还有 $u 个,分别是
for (( i = 0 ; $i < n; i=i+1 ))
doecho ${name1[$i]}
doneecho 删除成功请重新登录!
./denglu2.sh