简单功能实现,欢迎交流
#!/bin/bash
userData=/opt/app/SMS/V_SHELL/DB/user.dat
stuData=/opt/app/SMS/V_SHELL/DB/stu.dat
logDir=/opt/app/SMS/V_SHELL/LOG
theDate=`date '+%Y-%m-%d'`
count=0
######################################################
#read the fiel to check username and password #
#level one is administrator #
#level two is teacher #
#level three is student #
######################################################
function checkUser()
{
#init ret value is -1
ret=-1
while read line
do
user=`echo $line | awk -F: '{print $1}'`
passwd=`echo $line | awk -F: '{print $2}'`
level=`echo $line | awk -F: '{print $3}'`
active=`echo $line | awk -F: '{print $4}'`
# echo $user:$passwd:$level
if [[ $user = $1 && $passwd = $2 ]]
then
ret=$level
#################user have logged in##################
if [ $active -eq 1 ]
then
ret=4
else
echo $line
echo $user $passwd $level
sed -i "s/$line/$user\:$passwd\:$level\:1/g" $userData
fi
break
fi
done < $userData
return $ret
}
######################################################
#add the student info if the student is not exist #
#else #
#update the student info #
######################################################
function addStuData()
{
findStuFlag=0
echo -n "Input the student name:"
read name
echo -n "Input the math score:"
read mathScore
echo -n "Input the english score:"
read englishScore
echo -n "Input the science score:"
read scienceScore
while read line
do
nameStu=`echo $line | awk '{print $1}'`
if [[ $name = $nameStu ]]
then
findStuFlag=1
#########update the student info##############
sed -i "s/$line/$nameStu\t$mathScore\t$englishScore\t$scienceScore/g" $stuData
theTime=`date '+%Y-%m-%d %H:%M:%S'`
echo "$theTime@Teacher $1 add student $name information" >> $logDir/$theDate.log
fi
done < $stuData
if [ $findStuFlag -eq 0 ]
then
#########add the student info###############
echo -e "$name\t$mathScore\t$englishScore\t$scienceScore" >> $stuData
theTime=`date '+%Y-%m-%d %H:%M:%S'`
echo "$theTime@Teacher $1 add student $name information" >> $logDir/$theDate.log
fi
read -p "Press any key to continue..."
}
#################################################
#delete the student info as the student name #
#################################################
function delStuData()
{
findStuFlag=0
echo -n "Input the student name:"
read name
while read line
do
nameStu=`echo $line | awk '{print $1}'`
if [ $name = $nameStu ]
then
findStuFlag=1
sed -i "/^$name/d" $stuData
theTime=`date '+%Y-%m-%d %H:%M:%S'`
echo "$theTime@Teacher $1 delete student $name information" >> $logDir/$theDate.log
fi
done < $stuData
if [ $findStuFlag -eq 0 ]
then
echo "the student is not exist."
fi
read -p "Press any key to continue..."
}
###########################################
#add the user if the user is not exist #
#else #
#update the user info #
###########################################
function addUser()
{
findUserFlag=0
echo -n "Input user name:"
read name
echo -n "Input user password:"
stty -echo
read password
stty echo
echo
echo -n "Input user level(1~3):"
read level
while read line
do
userName=`echo $line | awk -F: '{print $1}'`
if [ $name = $userName ]
then
findUserFlag=1
#update the user info
sed -i "s/$line/$name\:$password\:$level\:0/g" $userData
fi
done < $userData
#add the user info
if [ $findUserFlag -eq 0 ]
then
echo "$name:$password:$level" >> $userData
theTime=`date '+%Y-%m-%d %H:%M:%S'`
echo "$theTime@Administrator $1 add user $name information" >> $logDir/$theDate.log
fi
read -p "Press any key to continue..."
}
############################################
#delete the user info as the user name #
############################################
function delUser()
{
findUserFlag=0
echo -n "Input user name:"
read name
while read line
do
userName=`echo $line | awk -F: '{print $1}'`
if [ $name = $userName ]
then
findUserFlag=1
#delete the user info
sed -i "/^$name/d" $userData
echo "$name is deleted..."
theTime=`date '+%Y-%m-%d %H:%M:%S'`
echo "$theTime@Administrator $1 delete user $name information" >> $logDir/$theDate.log
fi
done < $userData
if [ $findUserFlag -eq 0 ]
then
echo "the user $name is not exist"
fi
read -p "Press any key to continue..."
}
function inactiveUser()
{
while read line
do
name=`echo $line | awk -F: '{print $1}'`
if [ $name = $1 ]
then
echo $line
password=`echo $line | awk -F: '{print $2}'`
level=`echo $line | awk -F: '{print $3}'`
active=`echo $line | awk -F: '{print $4}'`
sed -i "s/$line/$name\:$password\:$level\:0/g" $userData
fi
done < $userData
}
function showAllUsers()
{
clear
echo "name password levelactive"
while read line
do
name=`echo $line | awk -F: '{print $1}'`
password=`echo $line | awk -F: '{print $2}'`
level=`echo $line | awk -F: '{print $3}'`
active=`echo $line | awk -F: '{print $4}'`
echo "$name $password $level$active"
done < $userData
read -p "Press any key to continue..."
}
function showOnlieUser()
{
clear
while read line
do
active=`echo $line | awk -F: '{print $4}'`
if [ $active -eq 1 ]
then
echo "name password levelactive"
name=`echo $line | awk -F: '{print $1}'`
password=`echo $line | awk -F: '{print $2}'`
level=`echo $line | awk -F: '{print $3}'`
echo "$name $password $level$active"
fi
done < $userData
read -p "Press any key to continue..."
}
function teacherMenu()
{
while true
do
clear
echo "Welcome $1 come back"
echo "1.query student score"
echo "2.add student score"
echo "3.delete student score"
echo "4.change the password"
echo "5.logout."
echo -n "Input your choice:"
read choice
case $choice in
1)
clear
echo "Name Math English Science"
cat $stuData
read -p "Press any key to continue..."
;;
2)
addStuData $1
;;
3)
delStuData $1
;;
4)
changePassword $1 $2
;;
5)
inactiveUser $1
theTime=`date '+%Y-%m-%d %H:%M:%S'`
echo "$theTime@$1 is logout..." >> $logDir/$theDate.log
break;
;;
*)
read -p "input is wrong, press any key to continue...."
;;
esac
done
}
function studentMenu()
{
while true
do
clear
echo "Welcome $1 come back"
echo "1.query score."
echo "2.change the password"
echo "3.logout."
echo -n "Input your choice:"
read choice
case $choice in
1)
queryStuData $1
;;
2)
changePassword $1 $2
;;
3)
inactiveUser $1
theTime=`date '+%Y-%m-%d %H:%M:%S'`
echo "$theTime@$1 is logout..." >> $logDir/$theDate.log
break
;;
*)
read -p "input is wrong, press any key to continue...."
;;
esac
done
}
function administratorMenu()
{
while true
do
clear
echo "Welcome administrator $1 come back"
echo "1.add the user"
echo "2.delete the user"
echo "3.display all users"
echo "4.display online users"
echo "5.change administrator password."
echo "6.logout."
echo -n "Input your choice:"
read choice
case $choice in
1)
addUser $1
;;
2)
delUser $1
;;
3)
showAllUsers $1
;;
4)
showOnlieUser $1
;;
5)
changePassword $1 $2
;;
6)
inactiveUser $1
theTime=`date '+%Y-%m-%d %H:%M:%S'`
echo "$theTime@$1 is logout..." >> $logDir/$theDate.log
break
;;
*)
read -p "input is wrong, press any key to continue...."
;;
esac
done
}
################################################
#query the srudent score as the student name #
################################################
function queryStuData()
{
while read line
do
#get the student the name,mathScore,englishScore,scienceScore
name=`echo $line | awk '{print $1}'`
mathScore=`echo $line | awk '{print $2}'`
englishScore=`echo $line | awk '{print $3}'`
scienceScore=`echo $line | awk '{print $4}'`
if [ $name = $1 ]
then
echo "Name Math English Science"
echo "$name $mathScore $englishScore$scienceScore"
fi
done < $stuData
read -p "Press any key to continue..."
}
function changePassword()
{
echo -n "Input old password:"
stty -echo
read oldpassword
stty echo
echo
echo -n "Input new password:"
stty -echo
read newpassword
stty echo
echo
echo -n "Verify your password:"
stty -echo
read newpassword1
stty echo
echo
if [[ $newpassword = $newpassword1 && $newpassword != "" ]]
then
if [[ $oldpassword = $2 ]]
then
#change the password
sed -i "s/$1:$oldpassword/$1:$newpassword/g" $userData
theTime=`date '+%Y-%m-%d %H:%M:%S'`
echo "$theTime@user $1 password is changed" >> $logDir/$theDate.log
else
echo "old password is wrong"
fi
else
echo "newpassword twice is not the same"
fi
read -p "Press any key to continue..."
}
function mainMenu()
{
while true
do
clear
echo "==========welcome to information system=========="
echo -n "username:"
read username
echo -n "password:"
stty -echo
read password
stty echo
echo
checkUser $username $password
ret=$?
case $ret in
1)
count=0
theTime=`date '+%Y-%m-%d %H:%M:%S'`
echo "$theTime@$username is login..." >> $logDir/$theDate.log
administratorMenu $username $password
;;
2)
count=0
theTime=`date '+%Y-%m-%d %H:%M:%S'`
echo "$theTime@$username is login..." >> $logDir/$theDate.log
teacherMenu $username $password
;;
3)
count=0
theTime=`date '+%Y-%m-%d %H:%M:%S'`
echo "$theTime@$username is login..." >> $logDir/$theDate.log
studentMenu $username $password
;;
4)
theTime=`date '+%Y-%m-%d %H:%M:%S'`
echo "$theTime@$username have logged in, please firstly logout..." >> $logDir/$theDate.log
exit
;;
*)
let count++
theTime=`date '+%Y-%m-%d %H:%M:%S'`
echo "$theTime@Warning:Input wrong username or password $count time, Please input again" >> $logDir/$theDate.log
;;
esac
if [ $count -eq 3 ]
then
theTime=`date '+%Y-%m-%d %H:%M:%S'`
echo "$theTime@Warning:You have input $count times wrong username and password" >> $logDir/$theDate.log
break
fi
done
}
theTime=`date '+%Y-%m-%d %H:%M:%S'`
echo "$theTime@Function is start ..." >> $logDir/$theDate.log
######################################################################
#change the directroy to the working directroy #
######################################################################
JOBS=$0
filename=`basename $0`
splitCount=`echo $0 | grep -c '/'`
if [ $splitCount -eq 0 ]
then
workDir='pwd'
else
workDir=`echo $0 | awk -F$filename '{print $1}'`
fi
cd $workDir
######################################################################
#shell script is running, exit #
######################################################################
if [ -e running ]
then
# exit 0
:
else
touch running
fi
mainMenu
####################################################################
#clear the running file #
####################################################################
if [ -e running ]
then
rm -rf running
fi
theTime=`date '+%Y-%m-%d %H:%M:%S'`
echo "$theTime@Function is end ..." >> $logDir/$theDate.log