简体   繁体   中英

Execute a command when another command is executed on linux

I was wondering if there is a command to "listen" to commands on Linux. What I mean, for every time a command is executed, for example sccs edit file, chmod +x file is executed

If this is just for your own use, you can create aliases or shell functions with the same name:

In bash:

alias ls="ls -l"

Then, whenever you do ls , it actually does ls -l (and any extra arguments are added after that).

Aliases are only useful to rename a command, and/or add a few initial parameters; you can't do anything more complicated. You can make the alias run a script, of course, and do anything you like that way.

Alternatively, you can create a shell function (put it in your .bashrc file, for example):

Again, in bash:

function sccs () {
   /usr/bin/sccs "$@"
   status=$?
   chmod +x "$1"
   return $status
}

I've no idea what an sccs command line looks like, so I expect you'd need to do something cleverer in there, but you get the idea, I hope.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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