简体   繁体   English

钩在终端上。 我可以在终端中运行命令之前调用方法吗?

[英]Hooks on terminal. Can I call a method before a command is run in the terminal?

I am wanting to make a terminal app that stores information about files/directories. 我想制作一个终端应用程序,用于存储有关文件/目录的信息。 I want a way to keep the information if the file is moved or renamed. 如果文件已移动或重命名,我想要一种保留信息的方法。

What I thought I could do is have a function execute before any command is run. 我以为我可以做的是在运行任何命令之前先执行一个函数。 I found this: 我找到了这个:

http://www.twistedmatrix.com/users/glyph/preexec.bash.txt http://www.twistedmatrix.com/users/glyph/preexec.bash.txt

But I was wondering if this would be a good way to go about it. 但是我想知道这是否是解决问题的好方法。 Or should I do something else? 还是我应该做别的事情?

I would like to call that function from a C program whenever mv is entered I suppose. 我想只要输入mv就可以从C程序调用该函数。

If what you're trying to do is attach some sort of metadata to files, there's a much better supported way to do that -- extended attributes . 如果您要执行的操作是将某种元数据附加到文件中,则有一种更好的支持方式- 扩展属性

Another solution might be to use the file's inode number as an index into a database you maintain yourself. 另一个解决方案可能是使用文件的索引节点号作为您自己维护的数据库的索引。

Can you alias the mv command? 您可以为mv命令加上别名吗? in .profile or .bashrc 在.profile或.bashrc中

alias mv=/usr/bin/local/mymv

where mymv is a compiled executable that runs your C code function and calls /usr/bin/mv. 其中,mymv是运行C代码函数并调用/ usr / bin / mv的已编译可执行文件。

precmd and preeexec add some overhead to every bash script that gets run, even if the script never calls mv. precmd和preeexec给运行的每个bash脚本增加了一些开销,即使该脚本从不调用mv。 The downside to alias is that it requires new code in /usr/local and if scripts or users employ /usr/bin/mv instead of mv it will not do what you want. 别名的缺点是,它需要/ usr / local中的新代码,并且如果脚本或用户使用/ usr / bin / mv而不是mv,它将无法执行您想要的操作。 Generally doing something like this often means there is a better way to handle the problem with some kind of service (daemon) or driver. 通常,这样做通常意味着有更好的方法来处理某种服务(守护程序)或驱动程序。 Plus, what happens if your C code cannot correctly handle interesting input like 另外,如果您的C代码无法正确处理有趣的输入(如

mv somefille /dev/null

If you want to run command each time after some command was executed in the terminal, just put the following in ~/.bashrc : 如果要在终端中执行某些命令后每次运行命令,只需将以下内容放入~/.bashrc

PROMPT_COMMAND="your_command;$PROMPT_COMMAND"

If you want your command to be executed each time before mv is executing, put the following in ~/.bashrc : 如果您希望在执行mv之前每次都执行命令,请将以下内容放入~/.bashrc

alias mv="your_script"

Make sure that your script will execute real mv if needed. 如果需要,请确保您的脚本将执行真正的mv

You can use inotify library to track filesystem changes. 您可以使用inotify库来跟踪文件系统更改。 It's good solution, but once user remove file, it's already gone. 这是一个很好的解决方案,但是一旦用户删除文件,它就已经消失了。

You might be able to make use of the DEBUG trap in Bash. 您也许可以使用Bash中的DEBUG trap

From man bash : 来自man bash

If a sigspec is DEBUG, the command arg is executed before every simple command, for command, case command, select command, every arithmetic for command, and before the first command executes in a shell function 如果sigspec是DEBUG,则在每个简单命令(对于命令,case命令,select命令,每个命令的算术)之前以及在shell函数中执行第一个命令之前,都要执行命令arg

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM