|
发表于 2003-10-18 02:36:34
|
显示全部楼层
del-and-recover 0.52 to Linux
特别感谢作者: alphatan
发信人: alphatan ([`a:lfa:ta2n]), 信区: LinuxApp
标 题: Re: del-and-recover 0.52 (1-3 del)
发信站: BBS 水木清华站 (Sat Oct 18 04:44:43 2003), 转信
写了两天,终于写完了,基本可实现如win下的“回收站”功能。
del: 删除东西用的。
del {路径}
路径允许通配符,允许多个路径,允许相对与绝对路径混用
lstrash: 看自己的垃圾筒内有什么。当然,一如“回收站”,如果你自己硬塞进去
的东西它是记录不了的。
lstrash [-n]
显示垃圾筒里的东西,参数-n用于显示其在记录文件中的行号。
undel: 恢复用。
undel {-ln LINENUMBER | -tn TRASHNAME | -sp SOURCEPATH}
-ln LINUENUMBER LINENUMBER为lstrash -n显示的行号
-tn TRASHNAME TRASHNAME,文件在垃圾筒内的名字,记录文件的第一列。
-sp SOURCEPATH SOURCEPATH,删除前的原文件的绝对路径--能力有限,不能确定唯一的绝对跟径……--也就是记录文件的第二加第三列形成的路径。
【 在 alphatan ([`a:lfa:ta2n]) 的大作中提到: 】
: #!/bin/bash
: #
: # "delset" is a set of scripts written in bash to simulate the win
: # version of del-and-recover to Linux. This is the content of 'del'
: # Copyright (C) 2003 alphatan<alphatan@263.net> version 0.52
: #
: # This program is free software; you can redistribute it and/or modify
: # it under the terms of the GNU General Public License as published by
: # the Free Software Foundation; either version 2 of the License, or
: # (at your option) any later version.
: #
: ...................
--
Learning is to improve, but not to prove.
※ 来源:·BBS 水木清华站 smth.org·[FROM: 218.19.45.30]
发信人: alphatan ([`a:lfa:ta2n]), 信区: LinuxApp
标 题: del-and-recover 0.52 (1-3 del)
发信站: BBS 水木清华站 (Sat Oct 18 04:41:29 2003), 转信- #!/bin/bash
- #
- # "delset" is a set of scripts written in bash to simulate the win
- # version of del-and-recover to Linux. This is the content of 'del'
- # Copyright (C) 2003 alphatan<alphatan@263.net> version 0.52
- #
- # This program is free software; you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation; either version 2 of the License, or
- # (at your option) any later version.
- #
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with this program; if not, write to the Free Software
- # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- #
- # Your *preparation* comes here
- declare -i NumberOfsourceFiles
- declare -f cleanTrash trashIsFull processFile appendRecordANDremoveFile
- declare -a sourceFiles
- # 对@, 1, 2, 3...之类的变量越早操作越好。
- sourceNumber=${#@}
- # 以下对IFS的操作比较简单,不会引起异常。
- tempIFS=$IFS
- IFS=$';'
- sourceFiles=( $@ )
- IFS=$tempIFS
- unset tempIFS
- NumberOfsourceFiles=${#sourceFiles[@]}
- # function definition begin
- function appendRecordANDremoveFile()
- {
- declare baseName dirName extName
- dirName="$1"
- baseName="$2"
- if [ "${baseName#*.}" = "${baseName}" ]; then
- extName=""
- else
- extName=".${baseName#*.}"
- fi
- # 这里可以加个测试,看.Trashdbm的第一栏中有没有跟filenameInTrash一样的
- # 文件名,如果有的话就再提一次随机数。
- # 我这里没设的原因是,既然人家叫得说是随机数,应该相同率起码小于
- # 1,000,000吧,而每次都在这测试的话,又是读文件,又是调用其它程序的,
- # 浪费太多资源,所以省了这步。要求高的自己写上吧。
- filenameInTrash="$(echo $RANDOM)$extName"
- if [ -e ${HOME}/.Trash/.Trashdbm ] ; then
- if [ -w ${HOME}/.Trash/.Trashdbm ] ; then
- if mv "$dirName/$baseName" "${HOME}/.Trash/$filenameInTrash" ; then
- echo -e "$filenameInTrash;$dirName;$baseName" >>$HOME/.Trash/.Trashdbm
- else return 1
- fi
- else
- echo -e "I can NOT write the Record file!" >&2
- return 1
- fi
- elif [ -w ${HOME}/.Trash/ ] ; then
- if mv "$dirName/$baseName" "${HOME}/.Trash/$filenameInTrash" ; then
- echo -e "$filenameInTrash;$dirName;$baseName" >>$HOME/.Trash/.Trashdbm
- else return 1
- fi
- else
- echo -e "I can NOT write the Record file!" >&2
- return 1
- fi
- }
- # processFile不可以有return--否则会停了大while了。其返回值为离开本函数前最后执行的命令。
- function processFile()
- {
- declare i baseName dirName
- declare -i j=0
- while (( $j < ${NumberOfsourceFiles} )) ; do
- i="${sourceFiles[$j]}" # 虽然多了个变量,但程序看起来舒服一些。
- if [ "${i#/}" = "${i}" ]; then # 测试参数给出的是绝对路径还是相对路径
- i="$(pwd)/$i" # 形成绝对路径 本为相对路径
- dirName=$(dirname "$i")
- baseName=$(basename "$i")
- if [ -e "$i" ] && [ -w "${dirName}" ]; then # 测试是否可删
- appendRecordANDremoveFile "$dirName" "$baseName"
- else
- echo -e "!!ERROR!!\n"$i" NOT-exist or NOT-writable!" >&2
- fi
- else
- i="$i" # 形成绝对路径 -- 符合规划书
- dirName=$(dirname "$i")
- baseName=$(basename "$i")
- if [ -e "$i" ] && [ -w "${dirName}" ]; then # 测试是否可删
- appendRecordANDremoveFile "$dirName" "$baseName"
- else
- echo -e "!!ERROR!!\n"$i" NOT-exist or NOT-writable!" >&2
- fi
- fi
- let j++
- done
- }
- function cleanTrash()
- {
- if [ -x ${HOME}/.Trash ] && [ -w ${HOME}/.Trash ] ; then
- rm -rf ${HOME}/.Trash/*
- echo "" >${HOME}/.Trash/.Trashdbm
- return 0
- else
- echo -e "!!ERROR!!\nI could not open your TRASH directory!\n Cleaning course FAILED!" >&2
- return 1
- fi
- }
- function trashIsFull()
- {
- declare cleanResponse trashVolumn=$(du -sm ~/.Trash |awk '{print $1}')
- if [ $trashVolumn -ge 501 ]; then
- echo -n "!!ERROR!!\nYour Trash is out of 500M!! Clean ALL?(yes, or else)" >&2
- read cleanResponse
- cleanResponse=$(echo $cleanResponse |tr [A-Z] [a-z])
- if [ "$cleanResponse" = 'yes' ] ; then
- if cleanTrash ; then
- if processFile ; then
- return 0
- else return 1
- fi
- else return 1
- fi
- else
- echo -e "I had NOT done ANY cleaning, Your Trash is still FULL!!" >&2
- return 0
- fi
- else
- if processFile ; then
- return 0
- else return 1
- fi
- fi
- }
- # function definition end
- # function *main* comes here
- trashIsFull
复制代码
--
Learning is to improve, but not to prove.
※ 来源:·BBS 水木清华站 smth.org·[FROM: 218.19.45.30]
发信人: alphatan ([`a:lfa:ta2n]), 信区: LinuxApp
标 题: del-and-recover 0.52 (2-3 lstrash)
发信站: BBS 水木清华站 (Sat Oct 18 04:42:11 2003), 转信- #!/bin/bash
- #
- # "delset" is a set of scripts written in bash to simulate the win
- # version of del-and-recover to Linux. This is the content of 'lstrash'
- # Copyright (C) 2003 alphatan<alphatan@263.net> version 0.52
- #
- # This program is free software; you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation; either version 2 of the License, or
- # (at your option) any later version.
- #
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with this program; if not, write to the Free Software
- # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- #
- # 这里不使用getopts,只有(只允许)一个选项,没必要用那个。
- declare showLineNumber="$1"
- if [ ! -r ${HOME}/.Trash/.Trashdbm ]; then
- exit 1
- fi
- if [ "$showLineNumber" = '-n' ] ; then
- awk -F\; 'BEGIN{counter=1}{print counter" : " $1" => "$2"/"$3; counter++;}' ${HOME}/.Trash/.Trashdbm
- elif [ "$showLineNumber" = '' ] ; then
- awk -F\; '{print $1" => "$2"/"$3}' ${HOME}/.Trash/.Trashdbm
- elif [ "$showLineNumber" = '--help' ] || [ "$showLineNumber" = '-h' ] ; then
- echo -e " Usage: ${0##*/} [-n]" >&2
- else
- echo -e "!!ERROR!!\nunknown Argument: "$showLineNumber" \n Usage: ${0##*/} [-n]" >&2
- fi
复制代码
--
Learning is to improve, but not to prove.
※ 来源:·BBS 水木清华站 smth.org·[FROM: 218.19.45.30]
发信人: alphatan ([`a:lfa:ta2n]), 信区: LinuxApp
标 题: del-and-recover 0.52 (3-3 undel)
发信站: BBS 水木清华站 (Sat Oct 18 04:42:57 2003), 转信
--
Learning is to improve, but not to prove.
※ 来源:·BBS 水木清华站 smth.org·[FROM: 218.19.45.30]
|
|