简体   繁体   English

如何使Shell在每个用户输入命令之前执行命令

[英]How to make shell execute a command prior to every user input command

I need a command to be executed before shell starts to execute the user passed command,i tried using trap with DEBUG signal, but that did not happen. 我需要在外壳程序开始执行用户传递的命令之前执行的命令,我尝试将DEBUG信号与trap一起使用,但这没有发生。

I have registered trap in /etc/profile. 我已经在/ etc / profile中注册了陷阱。

trap 'echo "my_command"' DEBUG 陷阱'echo“ my_command”'调试

Whats happening now is: 现在发生的是:

sw0:root:root> pwd sw0:root:root> pwd

/root /根

my_command my_command

sw0:root:root> SW0:根:根>

What i want is: 我想要的是:

sw0:root:root> pwd sw0:root:root> pwd

my_command my_command

/root /根

sw0:root:root> SW0:根:根>

Bash Version Used: Bash使用的版本:

GNU bash, version 2.04.0(1)-release (powerpc-unknown-linux-gnu) GNU bash版本2.04.0(1)-发行版(powerpc-unknown-linux-gnu)

I want my command to be executed prior to every command entered by user in shell,how do i do that? 我希望我的命令在用户在shell中输入的每个命令之前执行,我该怎么做?

Please help me on this,i tried lot of googling but that does not help. 请对此提供帮助,我尝试了很多谷歌搜索,但没有帮助。

You are probably looking for something like PROMPT_COMMAND : 您可能正在寻找类似PROMPT_COMMAND东西:

The contents of this variable are executed as a regular Bash command just before Bash displays a prompt. 在Bash显示提示之前,此变量的内容作为常规Bash命令执行。

My example 我的例子

function preexec() {
  echo "Preexec command"
}

trap 'preexec' DEBUG

Result is 结果是

# pwd
Preexec command
/home/robert

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

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