如何在进入文件夹时显示警告信息?

如何在进入文件夹时显示警告信息?

我不知道这是否可行,但是,我有一个文件夹,我想在用户进入时显示一些警告消息。在我看来,可以不经事先警告就删除该文件夹以节省一些磁盘空间。我已经在文件夹中创建了一个带有警告消息的文件:

WARNING!
##########################################################################################################################################################
Please, 
be advised, that the folder /company-backup/amazon-s3 can be deleted without previous WARNING to save disk space as the INFRASTRUCTURE TEAM judge necessary.

Best regards,
Infrastructure Team.
###########################################################################################################################################################

这可能吗?有什么想法吗?

答案1

如果您的用户通过命令行访问目录,则存在以下可能性:

cd在全局文件中重新定义 shell 的内置命令,以检查当前目录中是否存在rc文件,如果存在,则显示它(带有一些漂亮的光学元素)。.readme

A。通过zsh( /etc/zshrc) 你可以使用一个专门设计的钩子函数 ( chpwd),只要当前工作目录发生变化,它就会被调用:

function chpwd {
  if [[ -e .readme ]]; then
    echo
    echo \#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#
    cat .readme
    echo \#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#
    echo
fi }

b.我不知道在中是否存在类似的可能性bash,但是您可以cd在中重新定义/etc/bash.bashrc

cd() {
   builtin cd "$@"
   if [[ -e .readme ]]; then
     echo
     echo \#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#
     cat .readme
     echo \#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#
     echo
   fi
}

C。csh最后与( ) 语法相同/etc/csh.cshrc

alias cd 'cd \!*; eval "if (-e .readme) then \\
  echo \\
  echo ################################################################################ \\
  cat .readme \\
  echo ################################################################################ \\
  echo \\
endif"'

它看起来是这样的:

user@machine:~> cd temp

################################################################################
Attention! This is a temporary directory!
################################################################################

user@machine:~/temp> 

答案2

使用inotify 工具

$ inotifywait -e 打开你的目录

例子:

$ inotifywait -e open for_delete
Setting up watches.  
Watches established.
for_delete/ OPEN,ISDIR
$

上面的链接中有一个很好的脚本示例。

答案3

关于 zsh 的建议很好,但如果你想为 bash 做点什么,你可能需要自己编写脚本。我挂载到cdbash 中的命令是为了不同的目的(因此它会在终端窗口的标题中显示 PWD),但同样的技术可以用于.readmecd进入目录时 cat 文件。

我所做的就是编写一个脚本来执行cd(使用builtin关键字允许我覆盖实际命令 - 您也可以使用\cd),并在完成后执行我想要的任何操作。我将脚本存储在我的主目录中的某个地方(我~/scripts个人喜欢),然后在我的.bashrc文件中创建了一个别名来获取它。

echo在我看来,它看起来是这样的。对于你的情况,你可能想要的是而不是命令cat $PWD/.readme

〜/脚本/ cd.sh:

builtin cd $1
echo -ne "\033]0;$shell_title - `pwd | sed "s|$HOME|~|g"`\007"

〜/ .bashrc:

alias cd='source ~/scripts/cd.sh'

现在,每当我cd在终端中输入内容时,它都会调用该脚本,更改目录和窗口标题。

相关内容