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

举报
互联网老辛 发表于 2021/06/09 01:07:39 2021/06/09
【摘要】  #!/bin/bashsrcPath=""dstPath=""ErrMsg="Yout enter the directory or file dose not exist. "DstDirMsg="Please input destination dircetory"SrcFilesMsg="Please input source dircetory or f...


 
#!/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(){
while true
do
        echo $DstDirMsg
        read dstPath
        if [  -d $dstPath -a -e $dstPath ]; then

                break
        fi
        echo "$ErrMsg"
done
}

function copy(){
while true
do
        inputSourcePath
        inputDstPath


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

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

        fi

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

done

cp -a  $srcPath $dstPath
echo "The files have been susscessfully copied."

}

function delete(){

        inputSourcePath

        echo "Proceed with removal? y"
        read yn
        if [ $yn == "y" ]; then
                rm -rf $srcPath
                echo "sussessfully delete!"
        fi

}


function backup(){

        inputSourcePath
        inputDstPath

        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"
}


while true
do

echo "*******************************************"
echo " 1 Copy"
echo " 2 Delete"
echo " 3 Backup"
echo " 4 Quit "
echo "*******************************************"


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
        read -p "Press any key to continue..."
        clear
        ;;
        3)
        echo "You selection is Back"
        DirFileMsg=$SrcFilesMsg
        backup
        read -p "Press any key to continue..."
        clear
        ;;
        4)
        echo "Exit..."
        break
        ;;
        *)
        echo "Error! invalide selection, try again"
        ;;
esac
done


本文出自 “xinsz08の平行时空” 博客,请务必保留此出处http://xinsz08.blog.51cto.com/10565212/1915939

文章来源: zmedu.blog.csdn.net,作者:互联网老辛,版权归原作者所有,如需转载,请联系作者。

原文链接:zmedu.blog.csdn.net/article/details/77414677

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。