shell 编程实验--实现文件的备份和恢复

#########################################################################
# File Name: backup-restore.sh
# Author: HZG
# Created Time: Mon 26 Jun 2017 09:56:19 PM PDT
#########################################################################
#!/bin/bash

# 备份目录
backupdir ()
{
	dirtest
	echo "Backupping..."
	tar zcvf /tmp/backup.tar.gz $dir
}

# 恢复目录
restoredir ()
{
	dirtest
	echo "Restoring..."
	tar xvf /tmp/backup.tar.gz
}


# 验证目录
dirtest ()
{
	# 读目录名
	read dir
	# 判断是否是目录
	if [ ! -d $dir ]; then
		echo "Sorry $dir is not a directory!"
		exit 1
	fi
	cd $dir
}


choice=Y
while [ $choice == Y -o $choice == y ]
do
	# 打印菜单
	echo "====================================="
	echo "=        Backup-Restore Menu        ="
	echo "====================================="
	echo "+++++++++++++++++++++++++++++++++++++"
	echo "+         1:Backup  Directory       +"
	echo "+         2:Restore Directory       +"
	echo "+         0:Exit                    +"
	echo "+++++++++++++++++++++++++++++++++++++"
	echo -e "Please enter a choice (0--2):\c"

	
	# 判断用户输入
	read choice
	case "$choice" in
		1 ) echo -e "Please enter the directory name of backup file:\c"
			backupdir
			;;
		2 ) echo -e "Please enter the directory name of restore:\c"
			restoredir
			;;
		* ) echo "Invalid Choice!"
			exit 1
	esac

	# 判断操作是否成功
	if [ $? -ne 0 ]; then
		echo "Program encounter error!"
		exit 2
	else
		echo "Operate successfully!"
	fi

	# 提示是否继续
	echo -e "Would you like to continue ? Y/y to continue, ant other key to exit:\c"
	read choice
done

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章