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

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