LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-20-2009, 05:44 AM   #1
maradnus
Member
 
Registered: Oct 2008
Location: Yellagiri Hills
Distribution: Fedora
Posts: 87

Rep: Reputation: 15
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
 
Old 02-20-2009, 06:37 AM   #2
Udi
Member
 
Registered: Jan 2009
Posts: 165

Rep: Reputation: 44
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)?
 
Old 02-21-2009, 02:02 AM   #3
maradnus
Member
 
Registered: Oct 2008
Location: Yellagiri Hills
Distribution: Fedora
Posts: 87

Original Poster
Rep: Reputation: 15
Thanks for attending this query....

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


Thank you..
 
Old 02-21-2009, 03:52 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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
}
 
Old 02-21-2009, 06:25 AM   #5
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,078

Rep: Reputation: 195Reputation: 195
Building something like what you want in to the OS itself seems like a huge security issue.
 
Old 02-21-2009, 06:33 AM   #6
billymayday
LQ Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
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.
 
Old 02-21-2009, 06:36 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by arizonagroovejet View Post
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?
 
Old 02-21-2009, 06:43 AM   #8
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,078

Rep: Reputation: 195Reputation: 195
Quote:
Originally Posted by colucix View Post
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.
 
Old 02-21-2009, 06:56 AM   #9
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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
}
 
Old 02-21-2009, 11:48 AM   #10
lwasserm
Member
 
Registered: Mar 2008
Location: Baltimore Md
Distribution: ubuntu
Posts: 184

Rep: Reputation: 41
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)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to execute a ssh script on Linux server from Windows through a bat script? wanna13e Programming 13 10-23-2009 03:41 AM
Limited user cannot execute a script from within a script, but root can. versaulis SUSE / openSUSE 5 12-01-2008 01:08 PM
Apache 2.2: Execute CGI script from user directory jf.argentino Linux - Server 6 05-14-2008 04:21 AM
how to execute a script file? Have file/directory not found error sirius57 Linux - Software 2 11-22-2007 12:43 AM
How to execute/bring out a program without opening another terminal? f1l3t0f1sh Linux - Newbie 4 06-06-2005 03:53 PM


All times are GMT -5. The time now is 04:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration