系統的rm命令經常致使一些不愉快的事情。默認狀況下rm 不會在刪除文件前提是用戶是否真的想這麼作,刪除後也不能再從系統中恢復。這意味着用戶不得不爲本身的意識糊塗付出慘痛的代價。
設計一個相對「安全」的delete命令來替代rm。一下是代碼,請各位驗收
##創建回收站機制
##將須要刪除的文件移動到~/.trash中
if [ ! -d ~/.trash ]
then
mkdir ~/.trash
fi
if [ $# -eq 0 ]
then
#提示delete的用法
echo "Usage:delete file1 [file2 file3 ...]"
else
echo "You are about to delete these files:"
echo $@
#要求用戶確認是否刪除這些文件。回答N或n放棄刪除,其餘字符表示確認
read -p "Are you sure to do that? [Y/n]:" reply
if [ "$reply" != "n" ] && [ "$reply" != "N" ]
then
for file in $@
do
#判斷文件活目錄是否存在
if [ -f "$file" ] || [ -d "$file" ]
then
mv -b "$file" ~/.trash/
else
echo "$file: NO such file or directory"
fi
done
#若是用戶回答N或n
else
echo "NO file removed"
fi
fi
#
這個delete不完美, 他不能處理文件名中存在空格的狀況 # touch "zhang san" # delete zhang\ san You are about to delete these files: zhang san Are you sure to do that? [Y/n]:y zhang: NO such file or directory san: NO such file or directory