How to add a script call to a linux command?

2019-08-02 07:43发布

问题:

I want to add in my linux system a script call to the ls command. This script should be executed each time the user execute the ls command.

I tried 2 solutions but both are limited:

1) Using alias

alias ls="/root/myscript.sh; ls"

But this solution is limited because the user can call ls via a variable in this way

var="ls"
$var

see this link for more details

2) Using function

I create a function with the name ls:

ls() { /root/myscript.sh; /bin/ls $@ }

But this solution is limited because the user can call ls in this way:

/bin/ls

Are there another solution?

回答1:

You could always rename /bin/ls to /bin/something and create a shell script for /bin/ls and call the original there.

But be warned this can easly brick your system.



回答2:

You can put a script ls say in ${HOME}/myls/ls and then set the path variable to ${HOME}/myls:$PATH