linux - 禁用终端命令

标签 linux bash ubuntu-12.04 rm

<分区>

除某些情况外,我想禁用 rm 的使用。我在 .sh 文件中编写了一个名为 remove 的函数,它通过了我想在实际调用 rm 之前强加的某些检查。但是,仍然可以进入终端并简单地调用 rm 函数,而不是使用 remove。有没有办法禁用 rm 函数,除非被 remove 调用?我希望它就好像 rm 函数对登录到终端的用户不“存在”,所有“存在”的都是删除函数。

甚至可能更进一步,当用户调用 rm 时,它会在屏幕上打印一条语句,说明要使用 remove

作为一个更广泛的问题,除了在某些情况下,是否有办法禁用终端命令?我知道我可以为 rm 创建一个别名来要求 root,但这是一种简单但不太方便的方法。

#!/bin/bash

function rm {
  if [ $# -le 0 ]; then
    echo "Error: no arguments specified."
  else
    hasDir=0
    for arg in "$@"; do
      if [ -d "$arg" ]; then hasDir=1; fi
    done

    ac="Action canceled."
    resp=("y" "n" "e")

    sure=" "
    while [ "$sure" != "y" ] && [ "$sure" != "n" ]; do
      read -p "PERMANENT ACTION.  Are you sure? (y/n): " sure
    done

    if [ "$sure" == "n" ]; then echo "$ac"; return; fi


    if [ $hasDir -eq 1 ]; then
      direc=" "
      validResp=0
      while [ $validResp -eq 0 ]; do
        read -p "Remove all sub-directories? (y/n/e): " direc
        for ans in "${resp[@]}"; do
          if [ "$direc" == "$ans" ]; then validResp=1; fi
        done
      done

      if [ "$direc" == "e" ]; then echo "$ac"; return; fi
    else
      direc="n"
    fi



    check=" "
    validResp=0
    while [ $validResp -eq 0 ]; do
      read -p "Verify removal of each file? (y/n): " check
      for ans in "${resp[@]}"; do
        if [ "$check" == "$ans" ]; then validResp=1; fi
      done
    done

    if [ "$check" == "e" ]; then echo "$ac"; return; fi


    if [ "$direc" == "n" ]; then
      if [ "$check" == "n" ]; then
        for file in "$@"; do
          if [ ! -d "$file" ]; then command rm -f "$file"; fi
        done
      else 
        for file in "$@"; do
          if [ ! -d "$file" ]; then command rm -i "$file"; fi
        done
      fi
    else
      if [ "$check" == "n" ]; then
        command rm -rf "$@"
      else
        command rm -ir "$@"
      fi
    fi
  fi    
}

最佳答案

您可以覆盖 rm(或任何其他命令);内置的 command 允许您在必要时访问原始命令。

rm () {
    # Do something in addition to removing the file

    command rm "$@"
}

command 禁用 shell 函数查找。

关于linux - 禁用终端命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14905298/

相关文章:

C++ 程序在 Linux 上正确打开文件,但在 Windows 上却不能

linux - 如何抑制在 grep 中被视为选项的模式

bash - 使用管道代替时对 Sed 性能的影响;

linux - curl 命令不下载文件 (linux mint)

linux - 在 bash 中删除另一个文件中存在的内容

PHP后台作业到ubuntu xampp服务器,60-70分钟后被服务器中断

ubuntu - ubuntu 12.04 上的 Docker 守护进程启动错误

Clang 链接器问题(从源代码到 gcc-snapshot)

bash - 移动文件到目录

linux - 在第三个目录的两个文件之间创建符号链接(symbolic link)