Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need to get last 3 layer of the current directory 1

Status
Not open for further replies.

lmm8294

Technical User
Aug 20, 2001
8
0
0
US
I need to modify the .profile of an account, and I have this line in the .profile:
PS1=`hostname`':$LOGNAME:$PWD>'

But my boss want me to get only the last 3 levels of the subdirectory if `pwd` returns a long path. For example, if `pwd` returns /appl/oracle/instance001/d01/oracle/001appl/admin,

then he want only oracle/001appl/admin displayed. Could anybody help me? I have tried to use 'cut -d/ -f ...', but it does not work for me.


Thanks in advance.
 
It's going to slow things down for your users, but the only way I can think of doing this is to write a script pwd3 that will issue the pwd command, parse it to find the # of directories & then use cut to get the last 3.

Your PS1 line would then be :-

PS1=`hostname`':$LOGNAME:'`/usr/local/bin/pwd3`'>'

(or wherever you put the script). Obviously, having to run a script every time the prompt's shown will slow things down somewhat - make sure your users know it's your boss's idea X-) TandA
 
Thank you for your reply. Actually I was thinking about the method you suggest, but I am so stupid that I don't know how to get the number of '/' from `pwd`. It should be only several lines of code, but I could not make it work. Could you provide me with a sample? Many thanks.
 
See if the boss likes this :

PS1=`hostname`':$LOGNAME:$PWD
>'
That way the directory is on one line and the input line on the next line. Tony ... aka chgwhat

When in doubt,,, Power out...
 
Ummm, well - not yet having thrown off my REXX programming & gotten into Perl, you might need to go to the Perl forum & ask there how to loop through the path to get what you want :- forum219 TandA
 
Thank you very much for the help of chgwhat and Tanda. I will ask my boss if he likes chgwhat's suggestion and also I will go to visit forum 219.
 
Regarding Perl, you can use the following two lines in order to do your work (lines after the pound sign (#) are comments):
Code:
my $path = `pwd`;
# Following line leaves last three dir levels OTHERWISE changes nothing
$path =~ s/.*\/(.+\/.+\/.+)/$1/;
Jean Spector
QA Engineer @ mSAFE Ltd.
 
You can do the same thing with sed :

pwd | sed -e "s¤.*/\([^/]*/[^/]*/[^/]*\)$¤\1¤"


Are you sure you can put a command in PS1 a definition that is executed every time the prompt is displayed ?

In the definition

PS1=`hostname`':$LOGNAME:'`/usr/local/bin/pwd3`'>'

commands hostname and pwd3 are executed when PS1 is assigned, not when it is displayed.
Jean Pierre.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top