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?

2
  • 1
    To avoid recursion, change your function to ls() { /root/myscript.sh; command ls "$@"; } Commented Nov 28, 2014 at 18:05
  • Check PROMPT_COMMAND Commented Nov 29, 2014 at 4:10

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

If you do not alter /bin/ls, there is no way to prevent the user from invoking /bin/ls directly.
1

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

3 Comments

same problem of (2). I mean this solution is limited when the user call ls with /bin/ls
what are you trying to achieve?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.