[comp.unix.aix] How To: set PS1 to show users "current" directory?

mgphl@msa3b.UUCP (Michael Phillips) (04/12/91)

Trying to set up AIX on a PS/2 & have users that want their command
prompt to show their current directory (aka: $P$G in **DOS).

Any help will be greatly appreciated.

Thanks,

Michael G. Phillips    |   Dun & Bradstreet Software, Inc
 (404)239-2766         |   {...}!gatech!nanovx!msa3b!mgphl

davec@shared.uucp (Dave Close) (04/13/91)

In article <1599@msa3b.UUCP> mgphl@msa3b.UUCP (Michael Phillips) writes:
>Trying to set up AIX on a PS/2 & have users that want their command
>prompt to show their current directory (aka: $P$G in **DOS).

If you are using an xterm, either SVr4 or aixterm, the following might
also be of use.  It doesn't make your prompt so verbose and is always
present.  If installed in the ENV file for ksh of all users, the display
even changes properly when you use 'su'.

_cd () {
  unalias cd
  cd ${@}
  alias cd=_cd
  if [ "$(whoami)" = "root" ] ; then
    pmt=\#
  else
    pmt=\$
  fi
  export PS1="^[]0;$(hostname -s):$(whoami)  ${PWD}^G${pmt} "
}
[ "${TERM}" = "aixterm" -o "${TERM}" = "xterm" ] && {
  alias cd=_cd
  typeset -fx _cd
  if [ "$(whoami)" = "root" ] ; then
    pmt=\#
  else
    pmt=\$
  fi
  PS1="^[]0;$(hostname -s):$(whoami)  ${PWD}^G${pmt} "
  export PS1
}

Note, replace the ^[ and ^G with actual escape and bell characters.
There are trailing spaces on the lines with pmt=.

-- 
       	Dave Close         Shared Financial Systems          Dallas
        	davec@shared.com       vmail +1 214 458 3850
        	uunet!shared!davec       fax +1 214 458 3876
	My comments are my opinions and may not be shared by Shared.

ed@bee.austin.ibm.com (Ed Holley) (04/15/91)

Under the ksh, you can use

PS1='${PWD}> '

To get the equivalent of $p$g in dos.


In article <1991Apr13.025012.13451@shared.uucp>, davec@shared.uucp (Dave
Close) writes:
> Path: awdprime!auschs!romp!cs.utexas.edu!uunet!shared!davec
> From: davec@shared.uucp (Dave Close)
> Subject: Re: How To: set PS1 to show users "current" directory?
> Keywords: AIX
> Organization: Shared Financial Systems, Dallas, TX
> 
> In article <1599@msa3b.UUCP> mgphl@msa3b.UUCP (Michael Phillips) writes:
> >Trying to set up AIX on a PS/2 & have users that want their command
> >prompt to show their current directory (aka: $P$G in **DOS).


disclaimer: all views expressed are mine, not my employers.
----------------------------------------------------------------
Ed Holley	IBM AWD
ibm net:	ed@bee.austin.ibm.com
vnet:		holley at austin
internet:	eholley@wombat.austin.ibm.com
uucp:		...!cs.utexas.edu!ibmchs!auschs!bee.austin.ibm.com!ed

woan@nowhere (Ronald S Woan) (04/16/91)

ed@bee.austin.ibm.com (Ed Holley) writes:
Ed> Under the ksh, you can use
Ed> PS1='${PWD}> '

Which presupposes you have ksh for the PS/2... We didn't ship it with
AIX PS/2 or AIX/RT, did we?
-- 
+-----All Views Expressed Are My Own And Are Not Necessarily Shared By------+
+------------------------------My Employer----------------------------------+
+ Ronald S. Woan                woan@cactus.org or woan@austin.vnet.ibm.com +
+ other email addresses             Prodigy: XTCR74A Compuserve: 73530,2537 +
--
+-----All Views Expressed Are My Own And Are Not Necessarily Shared By------+
+------------------------------My Employer----------------------------------+
+ Ronald S. Woan                woan@cactus.org or woan@austin.vnet.ibm.com +
+ other email addresses             Prodigy: XTCR74A Compuserve: 73530,2537 +

law@ioe.lon.ac.uk (Lindsay Wakeman) (04/16/91)

In csh .login I use:

  cd
  alias cd 'cd \!*; set prompt="$cwd :"'

JANET: law@uk.ac.lon.ioe			| Lindsay Wakeman
EARN/BITNET: law%ioe.lon.ac.uk@ukacrl.bitnet	| Institute of Education
INTERNET:law%ioe.lon.ac.uk@nsfnet-relay.ac.uk	| 20 Bedford Way London WC1H OAL
UUCP: !mcvax!ukc!educ-isis!law			| VOICE +44 71 636 1500 ext.512
-- 
JANET: law@uk.ac.lon.ioe			| Lindsay Wakeman
EARN/BITNET: law%ioe.lon.ac.uk@ukacrl.bitnet	| Institute of Education
INTERNET:law%ioe.lon.ac.uk@nsfnet-relay.ac.uk	| 20 Bedford Way London WC1H OAL
UUCP: !mcvax!ukc!educ-isis!law			| VOICE +44 71 636 1500 ext.512

eclarke@ibmpa.awdpa.ibm.com (Eric S. Clarke) (04/17/91)

In article <1991Apr13.025012.13451@shared.uucp>, davec@shared.uucp (Dave
Close) writes:
|> In article <1599@msa3b.UUCP> mgphl@msa3b.UUCP (Michael Phillips)
|> writes:
|> >Trying to set up AIX on a PS/2 & have users that want their command
|> >prompt to show their current directory (aka: $P$G in **DOS).
|> 
|> If you are using an xterm, either SVr4 or aixterm, the following
|> might
|> also be of use.  It doesn't make your prompt so verbose and is
|> always
|> present. 
|> 
|> _cd () {
|>   unalias cd
|>   cd ${@}
|>   alias cd=_cd
|>   if [ "$(whoami)" = "root" ] ; then
|>     pmt=\#
|>   else
|>     pmt=\$
|>   fi
|>   export PS1="^[]0;$(hostname -s):$(whoami)  ${PWD}^G${pmt} "
|> }
|> [ "${TERM}" = "aixterm" -o "${TERM}" = "xterm" ] && {
|>   alias cd=_cd
|>   typeset -fx _cd
|>   if [ "$(whoami)" = "root" ] ; then
|>     pmt=\#
|>   else
|>     pmt=\$
|>   fi
|>   PS1="^[]0;$(hostname -s):$(whoami)  ${PWD}^G${pmt} "
|>   export PS1
|> }
|> 
|> Note, replace the ^[ and ^G with actual escape and bell characters.

Here are a couple of things to consider.  First, since the Korn shell 
reevaluates PS1 every time before displaying a new prompt there is no need to
alias cd.  This can be made as simple as setting the value of PS1.  Since this
is also probably the same env file that gets executed if you use dialup lines,
(That is when X is not useable) check to see if you have an xterm or aixterm.
If you don't set the value of PS1 to display on the command line.  If you do
set similarly to the above example.  There are a few differences that I would
like to point out.

If you change ^[]0 to ^[]1 you can control the icon string and then if you
change it to ^[]2 you can control the title string.  By puting both of these
in your prompt you can seperatly control the name in the title and the icon.

The next issue deals with command line editing in the Korn shell.  The shell
determines where to scroll the command line based on the width of the window
less the number of characters in the prompt.  This means that if you just put
a long PWD path and other misc. stuff in your title bar, the command line
starts to scroll long before it reaches the right margin.  You can fake out the
Korn shell by sending it an ^[[A.  This effectively restarts the count on the
length of the prompt.

So putting it all togeather (Assuming that the various variables are already
set:

case "${TERM}" in
   *xterm)
      export
PS1='^[]1;${HOSTNAME}^G^[]2;${USER}@${HOSTNAME}:${PWD}^G^[[A(!) $ '
      ;;
   *)
      export PS1='${USER}@${HOSTNAME}:${PWD} (!) $ '
esac

When this is displayed in your env file everything past ^[[A will be displayed 
on the next line.  As noted above ^[ = escape and ^G = control G (or Bell).  If
you wish you can change the $ at the end of the prompt to be a variable to 
indicate that the user is the root user.

	
-- 

Eric S. Clarke                     INTERNET:  eclarke@ibmpa.awdpa.ibm.com 
1510 Page Mill Road                    UUCP:  uunet!ibmsupt!eclarke
Palo Alto, CA 94304                IBM VNET:  ECLARKE at AUSVMQ
415-855-4458  T/L: 465-4458            

wolf@grasp1.univ-lyon1.fr (Christophe Wolfhugel) (04/17/91)

Ed> Under the ksh, you can use
Ed> PS1='${PWD}> '

Under csh this could be:
alias cd 'cd \!*;set prompt="`env LOGNAME`@`hostname`:$cwd> "'

Could be added to the FAQ... Don't move I'll send it to the person who proposed
to maintain the FAQ database.

-- 
Christophe Wolfhugel (on irc: Zolf)  |  Email: wolf@grasp1.univ-lyon1.fr
INSA Lyon - Departement Informatique |  "Ecole publique, ecole fauchee. Encore"
69621 Villeurbanne Cedex             |  "une tradition francaise dont on se"
France                               |  "passerait bien."

rol@grasp1.univ-lyon1.fr (Paul Rolland) (04/17/91)

In article <1991Apr16.093733.19362@ioe.lon.ac.uk> law@ioe.lon.ac.uk (Lindsay Wakeman) writes:
>
>In csh .login I use:
>
>  cd
>  alias cd 'cd \!*; set prompt="$cwd :"'
>
   That works fine, but I'd like to have the working directory relative
to my HOME directory. That's is : my HOME is /u/rol. If I'm in /u/rol/tmp/c,
I'd like to have the prompt being tmp/c, or even better, HOME/tmp/c when I'm
in /u/rol/tmp/c and /u when I'm in /u...
  How can I do that ?

	Paul.

 Paul Rolland, engineer [I hope :)] in July 91,
 Specialist of assembly on PC [Not too bad in fact :)],
 Trying to progress on Unix administration
 					   Email : rol@grasp1.univ-lyon1.fr 

looi@sutro.SFSU.EDU (W. W. Looi) (04/18/91)

In article <1991Apr17.144920.17478@grasp1.univ-lyon1.fr> rol@grasp1.univ-lyon1.fr (Paul Rolland) writes:
>In article <1991Apr16.093733.19362@ioe.lon.ac.uk> law@ioe.lon.ac.uk (Lindsay Wakeman) writes:
>>
>>In csh .login I use:
>>
>>  cd
>>  alias cd 'cd \!*; set prompt="$cwd :"'
>>
>   That works fine, but I'd like to have the working directory relative
>to my HOME directory. That's is : my HOME is /u/rol. If I'm in /u/rol/tmp/c,
>I'd like to have the prompt being tmp/c, or even better, HOME/tmp/c when I'm
>in /u/rol/tmp/c and /u when I'm in /u...
>  How can I do that ?
>
>	Paul.
>

I can deliver what you ask for if you can convince us the justification
for relative directory instead of absolute directory... :-)

ttfn - looi

mgphl@msa3b.UUCP (Michael Phillips) (04/20/91)

Just a quick Thank You to every one that responded to my query.
The *real* problem, however, is that AIX (for the PS/2) does NOT include
Korn shell....   oh well...  maybe the "users" will learn how to pwd before
they do something silly....

Thanks again,
Michael

martelli@cadlab.sublink.ORG (Alex Martelli) (04/26/91)

rol@grasp1.univ-lyon1.fr (Paul Rolland) writes:
	...
:   That works fine, but I'd like to have the working directory relative
:to my HOME directory. That's is : my HOME is /u/rol. If I'm in /u/rol/tmp/c,
:I'd like to have the prompt being tmp/c, or even better, HOME/tmp/c when I'm
:in /u/rol/tmp/c and /u when I'm in /u...

This is my approach in ksh (in the file that's in envir. var ENV):
---start cut:
tradir()
{
	echo $* | sed -e 's,^'$HOME',~,' -f $HOME/.dirsed
}
dirnam()
{
	if [ -r .dirnam ]
	then cat .dirnam
	else tradir $PWD
	fi
}
setpro()
{
	export PS1="${LOGNAME}_$THOST `dirnam` [!] "
}
mycd()
{
	'cd' $*; setpro
}
alias cd=mycd
---end cut
and in $HOME/.dirsed, I have:
---start cut
s,^/\([a-z0-9]*\)root/,\1:,
---end cut
That's a bit elaborate, but I like it!  If a directory contains a file named
.dirnam, then the contents of that file are to be displayed as the name of
the directory; else, a leading instance of the user's home directory, if any,
is translated to tilde, then sed is passed a user's personalized editing
script for directories-in-prompt, which in my case just translates 'root' 
into ':' if the leading directory is a child of / and its name ends with
'root' (that's because we nfs-mount pippo:/ as /pipporoot...).
setpro is a separate shell function because it can be used not only after
a cd but also after a push-directory, pop-directory, etc.
I'm not using the ksh's option to just expand a variable such as ${PWD} in
the prompt, because I want to do (potentially) rather sophisticated editing,
which can be a bit costly, and I want to pay that cost only at a cd rather
than over and over again!
-- 
Alex Martelli - CAD.LAB s.p.a., v. Stalingrado 53, Bologna, Italia
Email: (work:) martelli@cadlab.sublink.org, (home:) alex@am.sublink.org
Phone: (work:) ++39 (51) 371099, (home:) ++39 (51) 250434; 
Fax: ++39 (51) 366964 (work only), Fidonet: 332/401.3 (home only).