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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

last three level of `pwd` 1

Status
Not open for further replies.

lmm8294

Technical User
Aug 20, 2001
8
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 because I don't know how to count the occurance of '/' from `pwd`. I posted this question on the UNIX script forum, and Tanda suggests me to ask help from this forum. I appreciate it if anybody can help me with this problem.


Thanks in advance.
 
I know that there must be a much more elegant way of doing this, but this should help.......

In the code, $string needs to be equal to the path you are trying to work with.....

Code:
$pos = length $string;
$count = 3;

while ($count>0) {
$char = substr $string, $pos, 1;
if ($char eq '/') {
	$count--
	}
	$pos--
	}
	$new = substr $string, ($pos + 1);

With this I am assuming that all of your paths will contain at least 3 '/' characters. If not, you will need to add a if statement to check $pos to find out when it reaches the beginning of the line and return whatever result you have up to that point....

Based on your example of
/appl/oracle/instance001/d01/oracle/001appl/admin
this will return
/oracle/001appl/admin

if you want to kill the first / to get
oracle/001appl/admin

change
$new = substr $string, ($pos + 1);
to
$new = substr $string, ($pos + 2);

As I said in the beginning of this, there has gotta be a better way, but this is what I came up with off the top of my head.

Let me know how it works :)
Jim
 
Another way of doing this is:

Code:
$path = "/some/path/to/a/directory";
@toks = split </>, $path;
@toks = splice( @toks, -3, 3 );
print join(&quot;/&quot;, @toks), &quot;\n&quot;;

See man perlfunc for an explanation of the splice function.

Cheers, NEIL :cool:
 
That's a pretty elegant solution!
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Thank you very much for Jim and ToolKit. It is really nice to get responses so quickly. All of your responses are really helpfule. I will try both ways and let you know if it works.


Thank you very much again.
 
I have tried the second one, the script it self works very well. Thank you very much.
But the problem is, it works only when I log on or su - to that user. The perl file /home/apoap003/test contains the code provided by ToolKit, and in the /home/apoap003/.profile I have PS1 defined as below:

PS1=`hostname`':$LOGNAME:'`/home/apoap003/test`'>'

It works if I log on and the .profile was sourced the first time, but does not work if I cd to other directories. But if I manually source the .profile after I cd to other directories like $ORACLE_HOME, it works.

Any suggestion?

Thank you very much again.
 
Hmm..

Perhaps you could define a function to do this for you? For example, in bash, I think something like this could work:

Code:
function cd() {
    builtin cd $1
    $cropped=$(/path/to/perl/cropper $1)
    PS1=&quot;${cropped}$ &quot;
}

I myself use a cd function to define one letter aliases for my favourite directories, eg:

Code:
function cd() {
    case $1 in
        a) builtin cd /LOCAL/awesome;;
        b) builtin cd /tmp/bogus;;
        e) builtin cd /tmp/EXCELLENT;;
    esac
}
I'm not in front of my UNIX box at the moment, so I can't test the code above. Tell me if you have any problems ;-)

Cheers, NEIL
 
And obviously my case statement would need:

Code:
   *) cd $1;;
esac

Happy coding. Neil s-)
 
It works fine. Thanks a lot. Really appreciate it.
 
If you use the bash-shell take a look at :

they got really cute prompts.

I use to display the full path in the prompt and also change to x-window-title to username&machine:path

part of my .bashrc - file:
PROMPT_COMMAND='echo -ne &quot;\033]0;`/usr/ucb/whoami`@${HOSTNAME}: ${PWD}\007&quot;'
PS1=&quot;\u@\h:\w > &quot;

if u wanna see only the last sub-directory use :
# PS1=&quot;[\u@\h \W]\\$ &quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top