1. 程式人生 > >shell編程_實現回收站,避免誤刪文件

shell編程_實現回收站,避免誤刪文件

pos 回收站 lin code glog ror bin 家目錄 date

代碼如下:

下載:https://gitee.com/LoongWang/shellrecycling.git

 1 #/usr/bin/env bash
 2 
 3 
 4 #定義
 5 #定義回收站目錄
 6 RecyclingDir=/usr/local/recycling/
 7 #定義回收站log文件
 8 RecyclingLog=/var/log/recyclinglog
 9 #定義回收站家目錄
10 UserRecyclingDir="/usr/local/recycling/$(whoami)"
11 #定義返回碼
12 ReturnCode=0
13 
14 #程序開始
15 #檢測主文件是否存在、沒有則新建
16 17 for DirName in $RecyclingDir $UserRecyclingDir 18 do 19 if [ ! -d $DirName ] 20 then 21 #將Log寫入文件中 22 echo "$(date +"%F %H:%M:%S") - Make Dirctory" >> $RecyclingLog 23 #創建目錄 24 mkdir $DirName 25 26 if [ $? -eq 0 ] 27 then 28 echo
"$(date +"%F %H:%M:%S") - Make Dirctory $DirName Ok" >> $RecyclingLog 29 else 30 echo "$(date +"%F %H:%M:%S") - Make Dirctory $DirName Fail" >> $RecyclingLog 31 echo "$(date +"%F %H:%M:%S") - Make Dirctory Fail" 32 33 ReturnCode=127 34 echo
-e "The Linux Comamnd is error , returncode:127\n" 35 echo "$(date +"%F %H:%M:%S") - Make Dirctory Fail" 36 echo "Your can see log the $RecyclingLog" 37 38 exit $ReturnCode 39 fi 40 41 fi 42 done 43 44 #移動目錄至回收站 45 if [ $# -eq 0 ] 46 then 47 echo "Usage:$0 file1 file2 file3 ..." 48 ReturnCode=1 49 else 50 #循環開始 51 for DelFile in $@ 52 do 53 #判斷是否存在文件 54 if [ -e $DelFile ] 55 then 56 echo -e "\n$(date +"%F %H:%M:%S") - Delete $DelFile" >> $RecyclingLog 57 mv $DelFile $UserRecyclingDir 58 59 #判斷命令是否執行成功 60 if [ $? -eq 0 ] 61 then 62 #命令執行成功 63 echo -e "\n$(date +"%F %H:%M:%S") - Delete $DelFile - OK" >> $RecyclingLog 64 echo -e "$(date +"%F %H:%M:%S") - Delete $DelFile - OK" 65 else 66 #命令執行失敗 67 echo -e "\n$(date +"%F %H:%M:%S") - Delete $DelFile - Fail" >> $RecyclingLog 68 echo -e "\n$(date +"%F %H:%M:%S") - Delete $DelFile - Fail" 69 ReturnCode=1 70 fi 71 else 72 #不存在文件 73 echo -e "\n$(date +"%F %H:%M:%S") - $DelFile is not exists" >> $RecyclingLog 74 echo -e "\n$(date +"%F %H:%M:%S") - $DelFile is not exists" 75 ReturnCode=1 76 fi 77 done 78 79 exit $ReturnCode 80 fi

shell編程_實現回收站,避免誤刪文件