1

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?

Community
  • 1
  • 1
MOHAMED
  • 41,599
  • 58
  • 163
  • 268

2 Answers2

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.

Eun
  • 4,146
  • 5
  • 30
  • 51
1

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

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • same problem of (2). I mean this solution is limited when the user call ls with `/bin/ls` – MOHAMED Nov 28 '14 at 17:07
  • 1
    what are you trying to achieve? – Iharob Al Asimi Nov 28 '14 at 17:09
  • Mohamed, you are right, but you're asking as if the person from which you want to prevent running LS directly, knows more about the OS than you do ? If that is the case, copy your custom script to /bin/ls ... if that is the kind of level we are talking here. Don't forget to convert your script into a binary file, and use touch to set the date to the date of what /bin/ls now has, so that it doesn't stand out between the other commands. – tvCa Nov 29 '14 at 19:48