对Linux的用户来说,最恐怖的估计就是使用rm命令了,稍微不注意就执行了rm -rf /,后果就是整个系统没了,哭死也没人能救得了你,so,rm急需改造一下,今天发上来的这个小工具就是在删除前做一下检查的,使用方法如下:
1.将下述代码写入文件:install_rm_gqz:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
#!/bin/bash #set -x CP=/bin/cp CHMOD=/bin/chmod des_path=/gqz ERR_CODE=1 function install_rm_gqz { typeset workpath=$(pwd) if [ ! -d ${des_path} ] then { mkdir ${des_path} } fi if [ "X${workpath}" != "X${des_path}" ] then { ${CP} -rf ${workpath}/rm ${des_path}/rm ${CP} -rf ${workpath}/install_rm_gqz ${des_path}/install_rm_gqz } fi ${CHMOD} 755 ${des_path}/rm alias rm='${des_path}/rm' ${CP} -rf /etc/profile /etc/profile.install_rm_gqz while read readline do { command=$(echo ${readline} | egrep "alias[ ]+rm='") if [ "X${command}" != "X" ] then { continue } fi echo ${readline} >> ${des_path}/tmp } done < /etc/profile mv ${des_path}/tmp /etc/profile } function uninstall_rm_gqz { typeset workpath=$(pwd) while read readline do { command=$(echo ${readline} | egrep "alias[ ]+rm='") if [ "X${command}" != "X" ] then { continue } fi echo ${readline} >> ${des_path}/tmp } done < /etc/profile mv ${des_path}/tmp /etc/profile alias rm='/bin/rm' command=$(rm -rf ${des_path}) } function main { if [ $# -ne 1 ] then { echo -e "\033[33m please input param install | uninstall \033[0m" exit 1 } elif [ "${1}X" == "installX" ] then { install_rm_gqz } elif [ "${1}X" == "uninstallX" ] then { uninstall_rm_gqz } else { echo -e "\033[33m Error input param, please input install | uninstall \033[0m" exit 1 } fi } main $@ exit $? |
2.将下述代码写入文件rm:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
#!/bin/bash #set -x RM=/bin/rm CP=/bin/cp CHMOD=/bin/chmod des_path=/gqz ERR_CODE=1 function rm_gqz { if [ $# -eq 0 ] then { echo -e "\033[31m\033[5m Must give one or more param! \033[0m" exit ${ERR_CODE} } fi typeset array=($@) for arr in ${array[@]} do { #echo ${arr} case "${arr}X" in "/X") echo -e "\033[31m\033[5m The direction '/' is not permited to delete! \033[0m" exit ${ERR_CODE} ;; "/rootX") echo -e "\033[31m\033[5m The direction '/root' is not permited to delete! \033[0m" exit ${ERR_CODE} ;; "/binX") echo -e "\033[31m\033[5m The direction '/bin' is not permited to delete! \033[0m" exit ${ERR_CODE} ;; "/bootX") echo -e "\033[31m\033[5m The direction '/boot' is not permited to delete! \033[0m" exit ${ERR_CODE} ;; "/etcX") echo -e "\033[31m\033[5m The direction '/etc' is not permited to delete! \033[0m" exit ${ERR_CODE} ;; "/devX") echo -e "\033[31m\033[5m The direction '/dev' is not permited to delete! \033[0m" exit ${ERR_CODE} ;; "/sbinX") echo -e "\033[31m\033[5m The direction '/sbin' is not permited to delete! \033[0m" exit ${ERR_CODE} ;; *) ;; esac } done ${RM} ${array[*]} } function main { rm_gqz $@ } main $@ exit $? |
3. 对上述两个脚本,添加可执行权限:chmod 755 <文件名>
4. 执行install_rm_gqz install就可以将rm安装起来;执行install_rm_gqz uninstall就可以删除该小工具
后续该脚本不断更新,欢迎尝试;