LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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, 01: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, 02:58 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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 03:33 AM. Reason: Reviewed posted code
 
  


Reply



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 09:33 AM
Bouncing specific domain with specific message dlublink Linux - Software 1 08-21-2006 02:29 PM
Searching a specific directory for a specific extension? RoaCh Of DisCor Linux - Newbie 3 08-13-2005 03:28 PM
How to hide and block a directory for a specific user hoolie_v Linux - Newbie 2 08-08-2004 07:14 AM
how to flash a message to a specific user on login mtest Programming 5 10-30-2003 01:36 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:23 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
Open Source Consulting | Domain Registration