好久没写博了
哈哈,最近在学习Linux
这是偶写的第一个shell脚本,是一个实现类似windows里的回收站的程序,可以避免误删文件,希望能够对大家有所帮助,当然自己练手是最重要的,呵呵
#!/bin/bash
alias rm=trash
alias urm=untrash
alias vtr=viewtrash
alias ctr=cleartrash
common_error_msg="failed, check the ~/.trash directory manul"
trash()
{
pwd=`pwd`
#get all command line params into array $params
#and all files into array $files
i=0
j=0
unset params
unset files
params={}
files={}
while [ $# -gt 0 ]
do
p="$1"
if [ "${p:0:1}" == "-" ]
then
params[$i]=$p
let i=i+1
else
files[$j]=$p
let j=j+1
fi
shift
done
#
file_params=${files[*]}
file_param_str=${file_params[0]}
rmdate=`date "+%Y-%m-%d_%H-%M-%S"`
mkdir ~/.trash/$rmdate
mv $file_param_str ~/.trash/$rmdate/
}
untrash()
{
mv ~/.trash/$rmdate/* $pwd/
if [ $? == 0 ]
then
rmdir ~/.trash/$rmdate
if [ $? != 0 ]
then
echo "trashed but remove old directory $common_error_msg"
fi
else
echo "untrash $common_error_msg"
fi
}
cleartrash()
{
unalias rm
`rm -rf ~/.trash/*`
if test $? != 0
then
echo "clear trash $common_error_msg"
fi
alias rm=trash
}
viewtrash()
{
ls ~/.trash/*
if test $? != 0
then
echo "trash empty"
fi
unset ret
}
使用的方法是
在$HOME里新建一个.trash目录
先在这个文件放在$HOME里,文件名是.rm
在~/.bashrc最好添加以后代码
. ~/.rm