Execute a script upon opening a directory..
Dear friends
Is there any way to execute a shell script while opening a particular directory at the command line? The purpose is to have an environment (environment variables) to work with the files in the directory... Any help in this regard will be appreciated... Thanks |
I don't believe that there is a way to trigger a script that will run whenever somebody enters a directory.
But why won't you define the environment variables in /etc/profile or ~/.bashrc and they will be available all the time (also if you didn't cd to the particular directory)? |
Thanks for attending this query....
I have to capture some data when the user is inside the directory only. Thank you.. |
You can substitute the cd builtin with a custom function. It can check the working directory and source an hidden file containing all the definitions you need. For example, suppose the directory is /home/user/testdir and inside it you put a file called .hidden, containing the specific settings:
Code:
function cd(){ |
Building something like what you want in to the OS itself seems like a huge security issue.
|
Look into incron. Incron is similar to cron, but monitors changes to files and directory contents. I'm not sure if it will do exactly what you want, but it's worth investigating.
For example, you can use it to monitor files in a directory an when a file is accessed or modified, perform some action. See how you go. |
Quote:
|
Quote:
I can;'t think og any specific examples off the top of my head but that by default the operating system would allow a script to run whenever a user entered a particular directory just seems like a Bad Idea. Much the same way that the Autoplay feature on Windows where by if you insert a CD/DVD then by default Windows will run a certain file on that disk without any consultation with the user is a Bad Idea because that file on the CD/DVD could do anything and the user wouldn't know. See the Sony rootkit fiasco for example, though obviously that was made possible but the fact that in Windows the default behaviour is that users have admin rights. |
I got the point. Anyway the OP just wanted to run its own script. If he is the administrator of the system, he can put the script somewhere in a place where normal users haven't got access. I hope it will not contain anything harmful for the users of his system. To avoid unwanted actions he can always prompt the user before running the actual code and act accordingly with the user choice, for example:
Code:
function cd(){ |
See of you can install a package called "dnotify" on your system. It hooks into the kernel's
file and directory handling routines and can call a script or command when a directory is accessed. From the man page: NAME dnotify - Execute a command when the contents of a directory change SYNOPSIS dnotify [OPTION]... DIRECTORY... [-e COMMAND...] DESCRIPTION This manual page document describes the dnotify command. The dnotify program executes COMMAND every time the contents of any of the specified directories change. If a command is not specified, ‘echo {}’ is assumed. What is considered a change is determined by the ‘--access’, ‘--mod‐ ify’, ‘--create’, ‘--delete’, ‘--rename’ and ‘--attrib’ options (see below). These options may be combined. If none of them are specified, create and delete are assumed. (more) |
All times are GMT -5. The time now is 09:43 PM. |