Shell Programming _ Implement Recycle Bin, avoid accidental deletion of files

Source: Internet
Author: User

The code is as follows:

Download: Https://gitee.com/LoongWang/shellrecycling.git

1#/usr/bin/EnvBash2 3 4 #定义5 #定义回收站目录6Recyclingdir='/usr/local/recycling/'7 #定义回收站log文件8recyclinglog='/var/log/recyclinglog'9 #定义回收站家目录TenUserrecyclingdir="/usr/local/recycling/$ (WhoAmI)" One #定义返回码 AReturncode=0 -  - #程序开始 the #检测主文件是否存在, no new -  -  forDirNameinch$RecyclingDir $UserRecyclingDir -  Do +     if[ ! -D $DirName] -      Then + #将Log写入文件中 A         Echo "$ (date +"%F%h:%m:%s")-Make dirctory">>$RecyclingLog at #创建目录 -         mkdir$DirName -      -         if[$?-eq0 ] -          Then -             Echo "$ (date +"%F%h:%m:%s")-Make dirctory $DirName Ok">>$RecyclingLog in         Else -             Echo "$ (date +"%F%h:%m:%s")-Make dirctory $DirName Fail">>$RecyclingLog to             Echo "$ (date +"%F%h:%m:%s")-Make dirctory Fail" +   -Returncode=127 the             Echo-E"the Linux comamnd is error, returncode:127\n" *             Echo "$ (date +"%F%h:%m:%s")-Make dirctory Fail" $             Echo "Your can see log the $RecyclingLog" Panax Notoginseng  - Exit $ReturnCode the         fi +  A     fi the  Done +  - #移动目录至回收站 $ if[$#-eq0 ] $  Then -     Echo "usage:$0 file1 file2 file3 ..."     -Returncode=1 the Else - #循环开始Wuyi      forDelfileinch[email protected] the      Do - #判断是否存在文件 Wu         if[ -e $DelFile] -          Then About             Echo-E"\n$ (date +"%F%h:%m:%s")-Delete $DelFile">>$RecyclingLog $             MV$DelFile $UserRecyclingDir -              - #判断命令是否执行成功 -             if[$?-eq0 ] A              Then + #命令执行成功 the                 Echo-E"\n$ (date +"%F%h:%m:%s")-Delete $DelFile-ok">>$RecyclingLog -                 Echo-E"$ (date +"%F%h:%m:%s")-Delete $DelFile-ok" $             Else the #命令执行失败 the                 Echo-E"\n$ (date +"%F%h:%m:%s")-Delete $DelFile-fail">>$RecyclingLog the                 Echo-E"\n$ (date +"%F%h:%m:%s")-Delete $DelFile-fail"  theReturncode=1 -             fi in         Else the #不存在文件 the             Echo-E"\n$ (date +"%F%h:%m:%s")-$DelFile is not exists">>$RecyclingLog About             Echo-E"\n$ (date +"%F%h:%m:%s")-$DelFile is not exists"  theReturncode=1 the         fi the      Done +      - Exit $ReturnCode the fi

Shell Programming _ Implement Recycle Bin, avoid accidental deletion of files

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.