[net.unix] Korn Shell Question

mike@ntvax.UUCP (10/22/86)

I have a Korn Shell question that perhaps someone can answer. I like to
have as my system prompt my current path. When I use csh, I set up my
.cshrc file with these entries:
  
set prompt = "! `pwd`}"  -- this establishes my prompt as my command number
			    and current path when i first initiate the shell

alias cd 'chdir \!:1; set prompt=\!\`pwd`}\'
			    this sets up my cd command to not only change
			    my working directory but to change the prompt.

I would like to have something similar to this in ksh but I'm not sure how
to change PSx on the fly. Has someone done something similar to this??

Any help would be greatly appreciated.


      
      |######################################|
      |#         J. Michael Flanery         #|
      |#        Computer Science Dept.      #|
      |#     North Texas State University   #|
      |#   UUCP: {ihnp4}!infoswx!ntvax!mike #|
      |######################################|

lat@druil.UUCP (TepperL) (10/30/86)

In article <22300001@ntvax>, mike@ntvax.UUCP writes:
> 
> I have a Korn Shell question that perhaps someone can answer. I like to
> have as my system prompt my current path. When I use csh, I set up my
> .cshrc file with these entries:
>   
> set prompt = "! `pwd`}"  -- this establishes my prompt as my command number
> 			    and current path when i first initiate the shell
> 
> alias cd 'chdir \!:1; set prompt=\!\`pwd`}\'
> 			    this sets up my cd command to not only change
> 			    my working directory but to change the prompt.
> 
> I would like to have something similar to this in ksh but I'm not sure how
> to change PSx on the fly.

In the version of ksh we run here, all you have to do is add to
your .profile:

	PS1='$PWD} '

The Korn shell keeps the name of the current directory in the
shell variable PWD.  Also, note that you don't need to update
PS1 every time you chdir.
-- 
Larry Tepper	    {ihnp4 | allegra}!drutx!druil!lat	+1-303-538-1759

carroll@snail.CS.UIUC.EDU (10/30/86)

	Ah! Finally a question I know something about. We have tried several
solutions here, and the one that we think works the best is to put into
your .kshrc (remember, to make that work in Kshell, you must have ENV set and
exported to the filename):

ChangeDir=cd
function change_dir
    {
    $ChangeDir $*
    PS1="(!:${PWD}) "
    }
alias cd=change_dir
cd .

	Using a variable to the cd inside the funtion is needed so that when
the .kshrc is re-executed, you don't get a "recursive function" error. Doing
the cd . changes the prompt to be right immediately after .kshrc executes.

levy@ttrdc.UUCP (Daniel R. Levy) (10/30/86)

In article <22300001@ntvax>, mike@ntvax.UUCP writes:
>
>I have a Korn Shell question that perhaps someone can answer. I like to
>have as my system prompt my current path. When I use csh, I set up my
>.cshrc file with these entries:
>  
>set prompt = "! `pwd`}"  -- this establishes my prompt as my command number
>			    and current path when i first initiate the shell
>
>alias cd 'chdir \!:1; set prompt=\!\`pwd`}\'
>			    this sets up my cd command to not only change
>			    my working directory but to change the prompt.
>
>I would like to have something similar to this in ksh but I'm not sure how
>to change PSx on the fly. Has someone done something similar to this??
>
>Any help would be greatly appreciated.
>
>
>      
>      |######################################|
>      |#         J. Michael Flanery         #|
>      |#        Computer Science Dept.      #|
>      |#     North Texas State University   #|
>      |#   UUCP: {ihnp4}!infoswx!ntvax!mike #|
>      |######################################|

In ksh, define (say) chdir as a shell function, then alias cd to it.
ksh aliases aren't recursive, so this works:

case $0 in
	*ksh)	PS1=!$PWD}
		chdir()
		{
		cd $1	# this shell function will return here if the cd fails
		PS1=!$PWD\}
		}
		alias cd=chdir
esac
-- 
 -------------------------------    Disclaimer:  The views contained herein are
|       dan levy | yvel nad      |  my own and are not at all those of my em-
|         an engihacker @        |  ployer or the administrator of any computer
| at&t computer systems division |  upon which I may hack.
|        skokie, illinois        |
 --------------------------------   Path: ..!{akgua,homxb,ihnp4,ltuxa,mvuxa,
	   go for it!  			allegra,ulysses,vax135}!ttrdc!levy

price@snail.CS.UIUC.EDU (10/31/86)

Ok, now, is it possible to chage PS1 on the fly when you're using the
Bourne shell?

jona@moss.ATT.COM (10/31/86)

In ksh this is really a simple thing to do and you can extend in to be even
more useful by using this:

PS1="\${PWD#\$HOME/}> "

What this does is remove your home directory name from the beginning of
the prompt if you are anywhere below your home directory.
This is really useful because when you are in your own
directory structure your prompt isn't quite as long - and since my home
directory is 14 characters long (including /'s) it is nice. 

Example: my home directory is '/m/swdev/jona' so if I am in /m/swdev/jona/bin
my prompt will be 'bin> ' instead of 'm/swdev/jona/bin> '.

carroll@snail.CS.UIUC.EDU (10/31/86)

	Well, that does seem to work. I remember it failing for csh, but
I guess that's because you have to use `pwd` instead of a variable.
	For the bourne shell, you can do the same thing, except in your
.profile instead of .kshrc, and use change_dir() instead of function
change_dir. Unfortunately, that means that it won't work for any forked
shells (such as out of notesfiles or whatever). Also, you have to use
`pwd` because I don't think that Bourne shell supports the var PWD.

adam@mtund.UUCP (Adam V. Reed) (11/02/86)

> I have a Korn Shell question that perhaps someone can answer. I like to
> have as my system prompt my current path. When I use csh, I set up my
> .cshrc file with these entries:
>   
> set prompt = "! `pwd`}"  -- this establishes my prompt as my command number
> 			    and current path when i first initiate the shell
> 
> alias cd 'chdir \!:1; set prompt=\!\`pwd`}\'
> 			    this sets up my cd command to not only change
> 			    my working directory but to change the prompt.
> 
> I would like to have something similar to this in ksh but I'm not sure how
> to change PSx on the fly. Has someone done something similar to this??
> 
>       |#         J. Michael Flanery         #|

Ksh maintains the path of the working directory in $PWD, so you can use
	PS1='! $PWD $ '
which gives you the history file line number, the path of the working
directory, and "$ " to remind you you're using ksh.
					Adam Reed (ihnp4!mtund!adam||attmail!adamreed)