`
斌强-朱
  • 浏览: 48517 次
社区版块
存档分类
最新评论

Shell 成绩管理系统v0.2

 
阅读更多


#!/usr/bash
#Output file save the students information
FILE=./student.db
#temp file
TMP=./.tmp
#This is menu
menu () {
	echo "************************************************"
		echo -e "\t1.Add a student information"
	echo -e "\t2.Query a student information" 
	echo -e "\t3.Delete a student information by ID"	
	echo -e "\t4.Screen clearing"
	echo -e "\t0.Exit this program"
	echo "************************************************"
	echo -n "Select:"
}

#Add a student infomation
add () {
#validate ID
	while [ 1 ];do
		echo -n "Student ID(001-999):"
		read id
	 	flag=`echo $id|grep -c "^[0-9]\{3\}$"`
		
		if [ $flag -eq 0 -o "$id" == "000" ] ; then
			echo "ID must 001-999"
			continue 
		fi
		flag2=`awk '{print $1}' $FILE | grep -c ^${id}$`

		if [ $flag2 -ge 1 ] ;then
			echo "ID:${id} is already exists!"
			continue
		fi	

		break
	done
#validate Name	
	while [ 1 ];do
		echo -n "Student name(only a-z,A-Z and within 5 characters):"
		read name
		#flag record match line number
		flag=`echo $name | grep -c "^[a-zA-Z]\{1,5\}$"`
		if [ $flag -eq 0 ] ; then
			echo "Name must within 5 characters only a-z,A-Z"
			continue
		else 
			#Name into the capital letters
			name=`echo $name | tr '[a-z]' '[A-Z]'`
			break
		fi
	done
#validate Sex	
	echo -n "Student sex(M or F,default is M):"
	read sex
	if [ "$sex" = "F" -o "$sex" = "f" ] ; then
		sex=F
	else 
		sex=M
	fi
#validate Class
	while [ 1 ]; do
		echo -n "Student class(01-99 default is NULL):"
		read class
		if [ "$class" == "" ];then
			class=NULL
			break
		fi
 		#Class match 00-99
		flag=`echo $class|grep -c "^[0-9]\{2\}$"`
#check ?
		if [ $flag -eq 0 -o "$class" == "00" ] ; then
			echo "class must 01-99"
		else
			break
		fi
	done

	echo -e "$id\t$name\t$sex\t$class" >> $FILE
	echo "Add student information success"
}

queryById () {
	echo -n "Student ID:"
	read id
	if [ "$id" == "" ];then 
		queryAll
		return
	fi
	echo -e "ID\tName\tSex\tClass"
	grep $id $FILE
	if [ $? != 0 ]; then
		echo "No student information!"
	fi
}

queryByKeyword () {
	echo -n "Keyword:"
	read key
	if [ "$key" == "" ]; then
		queryAll
		return
	fi
	echo -e "ID\tName\tSex\tClass"
	grep -i $key $FILE
	if [ $? != 0 ]; then
		echo "Can't find the matching items"
	fi
}

queryAll () {
	echo -e "ID\tName\tSex\tClass"
	grep ""  $FILE
	if [ $? != 0 ]; then
		echo "No records found"
	fi
}

query () {

	echo "1.Through the student ID to queries"
	echo "2.Througt the Keyword to queries"
	echo "3.Query all students information"
	echo -n "Select:"
	read option
	case $option in
	1) queryById ;;
	2) queryByKeyword ;;
	3) queryAll ;;
	*) queryAll ;;
	esac
}

#Delete student
delete () {
	echo "1.Delete by ID"
	echo "2.Delete containt ID"
	echo -n "Select:"
	read opt
	case $opt in
	1) deleteById ;;
	2) deleteMatchId ;;
	*) error ;;
	esac
}

#Delete student by ID
deleteById () {
	flag=`grep -n "$id" $FILE`
	if [ "$flag" == "" -o "$id" == "" ]; then
		echo "can't find ID=$id student"
		return 
	fi	

   	#get delete number
	num=`awk '{print $1}' $FILE | grep -n "${id}" |sed 's/:.*//g'`	
	echo -e "ID\tName\tSex\tClass"
	#print the delete number
	sed -n "${num}p" $FILE

	echo -n "Are you sure delete id(Y or N):"
	read sure
	if [ "$sure" == "Y" -o "$sure" == "y" ] ; then
		sed "${num}d" $FILE > $TMP
		cat $TMP > $FILE
		echo "Deleted successful"
	fi

}

#Delete student macth ID
deleteMatchId () {
	echo -n "Input the student ID:"
	read id

	echo -e "ID\tName\tSex\tClass"
	#The first domain macth ID
	awk -F"\t" '$1~ /'$id'/ {print $0}' $FILE
	echo -n "Delete all ?(Y or N):"
	read all
	if [ "$all" == "Y" -o "$all" == "y" ] ; then
		#get match number
		#num=`grep -n "" $FILE | awk '$1~/'$id'/' | sed 's/:.*//g'`
		#num=`awk '$1~ /'$id'/ {print $0}' $FILE | grep -n "" |sed 's/:.*//g'`	
			#sed "${num}d" $FILE > $TMP
			awk '$1!~/'$id'/ {print $0}' $FILE >$TMP
			cat $TMP > $FILE
		echo "Deleted successful"
	fi
	return
}

#exit program
quit () {
	cls
	echo "Thank you Bye!"
	exit
}

#Error info
error () {
	echo "Error input!"
}

#screen cleaning
cls () {
	clear
}

#read anykey
anykey () {
	echo -n "press Enter key to continue..."
	read anykey
	cls
}

#main
main () {
touch $FILE
while [ 1 ] ; do
	menu
	read number
	case $number in
	1) add ; anykey ;;
	2) query ; anykey ;;
	3) delete ; anykey ;;
	4) cls ;;
	0) quit ;;
	*) error ;;
	esac
done
}

main






分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics