echo “$name" > 文件名 文件的里原来的内容被覆盖
echo “helloworld” >> 文件名 新内容添加在原来的内容后
grep 关键字 文件名 搜索文件中有关键字的行 -i 无关大小写
-v 反向搜索 搜索不含关键字的行 可用来删除某一行信息
#!/bin/bash
#filename:studentscore
search()
{
clear
if [ ! -f student.txt ];then
echo have no record
sleep 2
return
fi
echo input your name:
read name
if [ -z $name ];then
echo you didnt input a name
echo input your name:
read name
fi
grep -i $name student.txt
case $? in
1)echo name not in record
;;
0)echo search success
;;
esac
sleep 2
}
add()
{
clear
if [ ! -f student.txt ];then
touch student.txt
fi
echo please add name and score:
read newname
echo $newname >> student.txt
sort -o student.txt student.txt
echo " add success "
sleep 2
}
delete()
{
clear
if [ ! -f student.txt ];then
echo no record can delete
sleep 2
return
fi
echo please input delete name:
read name
grep -i $name student.txt
case $? in
1)echo name not in record
;;
0)cp student.txt student1.txt
rm -f student.txt
grep -v "$name" student1.txt > student.txt
rm -f student1.txt
echo delete success
;;
esac
sleep 2
}
display()
{
clear
more student.txt
sleep 4
}
edit()
{
vim student.txt
}
c()
{
clear
}
quit()
{
clear
echo -e "\033[45;37m"
exit
}
echo -e "\033[30;32m"
while true
do
clear
echo "*****************************"
echo "*** student score menu ***"
echo "*****************************"
echo "#############################"
echo " 1-search "
echo " 2-add "
echo " 3-delete "
echo " 4-display all record "
echo " 5-edit "
echo " c-clear "
echo " Q-quit "
echo "#############################"
echo -e -n "please input your choice [ 1,2,3,4,5,Q,c ] : "
read choice
case $choice in
1)search
;;
2)add
;;
3)delete
;;
4)display
;;
5)edit
;;
c|C)c
;;
q|Q)quit
;;
*)echo error choice
sleep 2
clear
;;
esac
done