设计要求
在主shell脚本文件中(必须以menu命名)要有一个多操作选项的菜单以便用户从中选择。此菜单的主要内容有:
①显示当前所有用户的记录
②显示用户名和用户ID
③查询并显示特定用户的记录
④往passwd文件中增加新用户的记录
⑤从passwd文件中删除某个用户的记录
⑥···退出···
在目录中主要有四个文件,分别为:menu、passwd、add、delete。其中menu文件中的程序完成菜单1~3功能;add和delete脚本文件分别完成4、5。当用户做了选择且完成所选的操作后,可以再次显示主菜单以供用户做下一步选择。
要求:①用while循环和case条件语句完成本次课程设计
目录
add代码
echo "you are in the add"
read -t 15 -p "please input your ID:" ID
echo "your ID = $ID"
echo -e "\n"
read -t 15 -p "please input your name:" name
echo "your name = $name"
echo -e "\n"
read -t 30 -p "please input your statu:" statu
echo "your name = $name"
echo -e "\n"
userinfo="$ID-$name-$statu"
echo "$userinfo" >> passwd.txt
delete代码
echo "you are in the delete"
echo "从passwd文件中删除某个用户的记录"
read -t 30 -p "please input you want to delect ID:" ID
a=1
for line in `cat passwd.txt`
do
OLD_IFS="$IFS" #保存旧的分隔符
IFS="-"
arr=($line)
IFS="$OLD_IFS" # 将IFS恢复成原来的
if [ ${ arr[0]} -eq $ID ]
then
sed -i "${ a}d" passwd.txt
fi
let a+=1
done
echo "success delete!!"
echo "end show"
~
passwd.txt代码
02-b-student
03-c-teacher
04-d-people
05-g-student
~
menu代码
function add()
{
sh add
sleep 1
}
function delete()
{
sh delete
sleep 1
}
function showall()
{
echo "All userinfo"
for line in `cat passwd.txt`
do
echo "[===$line===]"
done
echo "end show"
sleep 1
}
function showuser()
{
echo "All ID and name"
for line in `cat passwd.txt`
do
OLD_IFS="$IFS" #保存旧的分隔符
IFS="-"
arr=($line)
IFS="$OLD_IFS" # 将IFS恢复成原来的
echo "[**ID=${ arr[0]}**name=${ arr[1]}**]"
done
echo "end show"
sleep 1
}
function finduser()
{
echo "查询并显示特定用户的记录"
read -t 30 -p "please input you want to find ID:" ID
for line in `cat passwd.txt`
do
OLD_IFS="$IFS" #保存旧的分隔符
IFS="-"
arr=($line)
IFS="$OLD_IFS" # 将IFS恢复成原来的
if [ ${ arr[0]} -eq $ID ]
then
echo "[**ID=${ arr[0]}**name=${ arr[1]}**statu=${ arr[2]}**]"
fi
done
echo "end show"
sleep 1
}
function menu()
{
cat << EOF