LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Execute a script upon opening a directory.. (https://www.linuxquestions.org/questions/linux-newbie-8/execute-a-script-upon-opening-a-directory-706142/)

maradnus 02-20-2009 04:44 AM

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

Udi 02-20-2009 05:37 AM

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)?

maradnus 02-21-2009 01:02 AM

Thanks for attending this query....

I have to capture some data when the user is inside the directory only.


Thank you..

colucix 02-21-2009 02:52 AM

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(){
  builtin cd "$@"
  if [ "$PWD" == /home/user/testdir ]
  then
    . .hidden
  fi
}


arizonagroovejet 02-21-2009 05:25 AM

Building something like what you want in to the OS itself seems like a huge security issue.

billymayday 02-21-2009 05:33 AM

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.

colucix 02-21-2009 05:36 AM

Quote:

Originally Posted by arizonagroovejet (Post 3452106)
Building something like what you want in to the OS itself seems like a huge security issue.

Huge? How can your own functions compromise security in a so heavy way? Having a function like that does not open any hole in the system, at least not one more than the hundreds of holes that already exist. Am I missing something?

arizonagroovejet 02-21-2009 05:43 AM

Quote:

Originally Posted by colucix (Post 3452114)
Huge? How can your own functions compromise security in a so heavy way?

I was not talking about 'your own functions' as in a functions which are defined by the user themselves. I was talking about having such a feature as part of the operating system.
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.

colucix 02-21-2009 05:56 AM

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(){
  builtin cd "$@"
  if [ "$PWD" == /home/user/testdir ]
  then
    read -p "Entering directory $HOME/testdir; do you want to execute script blah blah (y/n)? " ans
    if [[ "$ans" =~ [YyEeSs] ]]
    then
      . /path/to/.hidden
    else
      builtin cd -
    fi
  fi
}


lwasserm 02-21-2009 10:48 AM

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.