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