shell脚本示例,维护员工记录文件

本文介绍了一个简单的员工信息管理系统脚本,该系统使用shell脚本实现,包括添加、删除、搜索及显示记录等功能。用户可通过菜单选择操作,如输入新员工信息(姓名、电话等),搜索特定记录或删除指定记录。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#! /bin/sh

# To display Menu information
#
function displayMenu(){
	echo ""
	echo "Dominion Consulting Employees Info Main Menu"
	echo "============================================"
	echo "1 – Print All Current Records"
	echo "2 - Search for Specific Record(s)"
	echo "3 - Add New Records"
	echo "4 – Delete Records"
	echo "Q - Quit"
	echo ""
}

# Ask users whether to continue
#
function ifCon(){
	echo -e "Press Enter to continue...\c"
	read ifContinue
	
	if [ "$ifContinue" = "" ]
	then
		continue;
	fi
}

# Ask users to get phone number when add new employee records
#
function addPhone(){
	while true
	do
		echo -e "Phone Number (xxxxxxxx): \c"
		read phoneNum
		# Check if the phone number is not null, 8 digits, first digit nonzero and only number
		# and does not exist
		#
		if [ "$phoneNum" = "" ]
		then
			echo "Phone Number not entered"
			continue
		elif [[ $phoneNum = 0* ]] || [ ! `expr length $phoneNum` -eq 8 ] || [[ $phoneNum =~ [^0-9] ]]
		then
			echo "Invalid phone number"
			continue
		elif grep $phoneNum records>/dev/null
		then
			echo "Phone number exists"
			continue
		else
            break
		fi
	done
	echo ""
}

# Ask users to get Name or Job when add new employee records
# When use:
# 		addNameOrJob "Family Name" | "Given Name" | "Job Title" 
#
function addNameOrJob(){
	while true
	do
		echo -e "$1: \c"
		read inputName
		# Get user's input, which can contain only alphabetic characters and spaces
		#
		if [[ $inputName =~ [^a-zA-Z' '] ]]
    	then
    		echo "$1 can contain only alphabetic characters and spaces"
			continue
		else
		# Recording the second parameters, different instructions will be used
		#
			if [ "$1" = "Given Name" ]
			then
				givenName=$inputName
			elif [ "$1" = "Family Name" ]
			then
				familyName=$inputName
			elif [ "$1" = "Job Title" ]
			then
				jobTitle=$inputName
			fi
			break
		fi
	done
	echo ""
}

# Used to add new records, using values assigned before
#
function addNewEmployee(){
	echo "Adding new employee record to the records file ..."
	#Ex. 95673456:Couch:David:26:chef
	#
	newEmployee=$phoneNum":"$familyName":"$givenName":"$departNum":"$jobTitle
	if echo $newEmployee >> records
	then
		echo "New record saved"
	fi
	echo ""
}

# Used to add department information of new employee records
#
function addDepartment(){
	while true
	do
		echo -e "Department Number: \c"
		read departNum
		# department number should be 2 digits
		#
		if [[ $departNum = [0-9][0-9] ]]
    	then
    		break
		else
			echo "A valid department number contains 2 digits"
			continue
		fi
	done
	echo ""
}

# Ask users if delete the certain record
#
function deleteRecord(){
	grep $deletePhNum records
	echo ""
	
	echo -e "Confirm deletion: (y)es or (n)o: \c"
	read ifDeletion
	echo ""
	case "$ifDeletion" in
		y|Y) 
			# Delete certain record
			#
			grep -v $deletePhNum records > tran
			cat tran > records
			rm tran
			echo "Record deleted."
			break ;;
		n|N) break ;;
		*) echo "Invalid input"; continue ;;
	esac
}

# Check if the "records" file exits, if not, then say error message
#
if [ ! -f records ]
then
	echo "Error: There is no 'Records' file in the directory :("
	echo ""
	exit
fi

while true
do	
	# First, display the main Menu which can let users to select special operations
	# like add, delete, search for or display 
	#
	displayMenu
	echo -e "Your Selection: \c"
	read menus
	
	# 
	#
	case "$menus" in
		1) 
			cat records
			echo -e "\n"
			ifCon
			;;
		2)	
			# Keyword search
			#
			echo -e "Enter keyword: \c"
			read keyword

			if [ "$keyword" = "" ]
			then
				echo "Keyword not entered"
			elif grep $keyword records>/dev/null
			then
				grep $keyword records
			else				
				echo "$keyword not found"
			fi
			ifCon
			;;	
		3)
			# Use some predefined function to finish the "Add" operation
			#
			while true
			do
				echo "Add New Employee Record"	
				addPhone            
            	addNameOrJob "Family Name"
            	addNameOrJob "Given Name"
            	addDepartment
            	addNameOrJob "Job Title"
           		addNewEmployee
            
            	echo -e "Add another? (y)es or (n)o: \c"
            	read conti
            	case "$conti" in
					y|Y) continue ;;
					n|N) break ;;
					*) echo "Invalid input"; continue ;;
				esac
			done
			;;
		4)
			# Delete employee record
			#
			echo "Delete Employee Record"
			echo ""
			
			while true
			do
				echo -e "Enter a phone number: \c"
				read deletePhNum
				# Phone number should not be empty, length less than 8 or not exists
				#
				if [ "$deletePhNum" = "" ]
				then
					echo "Phone Number not entered"
					continue
				elif [[ $deletePhNum = 0* ]] || [ ! `expr length $deletePhNum` -eq 8 ]
				then
					echo "Invalid phone number"
					continue			
				elif ! grep $deletePhNum records>/dev/null
				then
					echo "Phone number not found"
					continue
				else
					deleteRecord
				fi
			done
			;;	
		q|Q)
			echo "Bye"
			exit
			;;
		*)
			echo "Invalid input"	
			;;	
	esac
done

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值