LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 05-13-2009, 02:27 AM   #1
mohit052
LQ Newbie
 
Registered: Nov 2008
Posts: 25

Rep: Reputation: 15
Printing a message on the console when the user moves to specific directory


Hi all .
I want that the user whenever moves to the directory /home/user/someApp
He should get a alert message

example:

[demo@localhost ~]$cd /home/user/someApp
ALERT!!!
Please dont delete any files in the folder...
[demo@localhost /home/user/someApp]$

Thanks in advance ... :-)
 
Old 05-13-2009, 03:58 AM   #2
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 have to create your own function and put it in a file which is sourced whenever a user logins or opens a new shell session. It depends on the system you're running on (e.g. /etc/bashrc, /etc/profile, etc).
Code:
function cd() {
  builtin cd "$@" && test "$PWD" == "/home/$USER/someApp" && echo ALERT\!\!\! && echo Please don\'t delete any files in the folder...
}
Edit: I was wondering about the possibility a user changes directly to a directory that is some levels down /home/user/someApp. In that case the message is not displayed unless you change the function to match $PWD against a regular expression. The following is valid for Bash version 3 only (version 2 does not have the =~ operator). For older versions you have to change the code using grep:
Code:
function cd() {
  builtin cd "$@"
  if [[ "$PWD" =~ "^$HOME/someApp" ]]
  then
    echo ALERT\!\!\!
    echo Please don\'t delete any files in the folder...
  fi
}
For bash version 2, change the if statement to
Code:
if echo "$PWD" | grep -q $HOME/someApp

Last edited by colucix; 05-13-2009 at 04:33 AM. Reason: Reviewed posted code
 
  


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
Tiger checks Ubuntu get fail message root directory owned by user argh2xxx Linux - Security 2 10-20-2008 10:33 AM
Bouncing specific domain with specific message dlublink Linux - Software 1 08-21-2006 03:29 PM
Searching a specific directory for a specific extension? RoaCh Of DisCor Linux - Newbie 3 08-13-2005 04:28 PM
How to hide and block a directory for a specific user hoolie_v Linux - Newbie 2 08-08-2004 08:14 AM
how to flash a message to a specific user on login mtest Programming 5 10-30-2003 02:36 AM


All times are GMT -5. The time now is 01:35 AM.

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