How to add a script call to a linux command?

那年仲夏 提交于 2019-12-11 07:53: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



来源:https://stackoverflow.com/questions/27193033/how-to-add-a-script-call-to-a-linux-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!