是否可以限制某些命令在目录中运行?

sda*_*das 4 command-line shell git

我有两个目录:srcprojects. 我想阻止自己跑步,git ...除非我特别在里面srcprojects. 这可能吗?

Hau*_*ing 7

很难阻止二进制文件运行,但对于典型情况,存在一种简单的保护方法:

  1. 您定义了一个覆盖名称的 shell 函数。这显然在另一个(也是另一个用户的)shell 中不起作用。
  2. 您从 $PATH 中取出二进制文件并用包装脚本替换它。这更安全,但显然会导致软件更新出现问题(脚本被覆盖)。

shell 函数可能如下所示:

git () {
  local cwd="$(pwd -P)"
  if ! [ "/path/to/src" = "$cwd" -o "/path/to/projects" = "$cwd" ]; then
    echo "The current working directory is: '${cwd}'"
    echo "git must not be run from here; from src and projects only."
    echo "Aborting."
  else
    command git "$@"
  fi
}
Run Code Online (Sandbox Code Playgroud)