天天看点

使用函数封装-公司自动化处理文件备份管理脚本

#!/bin/bash

srcPath=""

dstPath=""

ErrMsg="Yout enter the directory or file dose not exist. "

DstDirMsg="Please input destination dircetory"

SrcFilesMsg="Please input source dircetory or file."

DstFilesMsg="Delete the file or dircetory. "

DirFileMsg=""

function inputSourcePath(){

while true

do

        echo $DirFileMsg

        read srcPath

        if [  -d $srcPath -o -f $srcPath  ]; then

                if [ -e $srcPath ]; then

                        break

                fi

        fi

        echo "$ErrMsg"

done

}

function inputDstPath(){

        echo $DstDirMsg

        read dstPath

        if [  -d $dstPath -a -e $dstPath ]; then

                break

function copy(){

        inputSourcePath

        inputDstPath

        if [  $srcPath != $dstPath  -a  -d $srcPath  ]; then

        elif [ -f $srcPath -a  `dirname $srcPath` != $dstPath ]; then

        echo "The source and destination cannot be the same! Please input again"

cp -a  $srcPath $dstPath

echo "The files have been susscessfully copied."

function delete(){

        echo "Proceed with removal? y"

        read yn

        if [ $yn == "y" ]; then

                rm -rf $srcPath

                echo "sussessfully delete!"

function backup(){

        backName=`date -d today +%Y%m%d%H%M%S`

        cd $dstPath

        tar -czvf  "$backName.tar.gz" $srcPath

        echo "A successful backup to $dstPath/$backName.tar.gz"

echo "*******************************************"

echo " 1 Copy"

echo " 2 Delete"

echo " 3 Backup"

echo " 4 Quit "

read op

case $op in

        1)

        echo "You selection is Copy"

        DirFileMsg=$SrcFilesMsg

        copy

        read -p "Press any key to continue..."

        clear

        ;;

        2)

        echo "You selection is delete"

        DirFileMsg=$DstFilesMsg

        delete

        3)

        echo "You selection is Back"

        backup

        4)

        echo "Exit..."

        break

        *)

        echo "Error! invalide selection, try again"

esac

本文转自xinsz08の平行时空博客51CTO博客,原文链接http://blog.51cto.com/xinsz08/1915939如需转载请自行联系原作者

<b>维度2018</b>