[comp.unix.wizards] PWD in Bourne Shell PS1

chris@choreo.COM.COM (Chris Hare / System Manager) (01/07/90)

In article <8790.259b3142@ecs.umass.edu> Satam writes,
> Inserting the following three aliases will help achieve the same
> effect. Note that this is only for csh.
> ------------------------------------------------------------
> alias cd 'chdir \!* && set prompt="${cwd:t}_\\!% "'
> alias pushd 'pushd \!* && set prompt="${cwd:t}_\\!% "'
> alias popd 'popd \!* && set prompt="${cwd:t}_\\!% "'
> -------------------------------------------------------------

This is quite true.  However, a similar thing can be implemented in the
Bourne shell using shell functions.  There are however two caveats :

	1.  We cannot use the name of a built in shell command 
	2.  We must recaculate the PS1 each time we do a cd.

Firstly,  becase we want the PS1 to be updated whenever we change
directories, we cannot use the builtin 'cd' command, but something like it.
We must also remember to use the new command, because using a cd will mean
that the directory reflected in our PS1 will be incorrect.

You can try this Bourne shell function :

	go()
	   {
	   if [ "$1" ]		# if we got an argument
	   then
	      cd $1
	   else 		# otherwise go home
	      cd $HOME
	   fi
	   PS1=`pwd`"> "	# change the PS1
	   }

Remember, shell functions are not passed to their subshells.

Chris Hare				Choreo Systems Inc.
Coordinator, Systems Management		150 Laurier Ave West
Authorized SCO Instructor		Ottawa, Canada
					Phone 613-238-1050
					Fax   613-238-4453
*** It Works for ME ! ***		email : uunet!choreo!chris

mercer@ncrcce.StPaul.NCR.COM (Dan Mercer) (01/09/90)

In article <18@choreo.COM> chris@choreo.UUCP (Chris Hare / System Manager) writes:
:In article <8790.259b3142@ecs.umass.edu> Satam writes,
:> Inserting the following three aliases will help achieve the same
:> effect. Note that this is only for csh.
:> ------------------------------------------------------------
:> alias cd 'chdir \!* && set prompt="${cwd:t}_\\!% "'
:> alias pushd 'pushd \!* && set prompt="${cwd:t}_\\!% "'
:> alias popd 'popd \!* && set prompt="${cwd:t}_\\!% "'
:> -------------------------------------------------------------
:
:This is quite true.  However, a similar thing can be implemented in the
:Bourne shell using shell functions.  There are however two caveats :
:
:	1.  We cannot use the name of a built in shell command 
:	2.  We must recaculate the PS1 each time we do a cd.
:
:Firstly,  becase we want the PS1 to be updated whenever we change
:directories, we cannot use the builtin 'cd' command, but something like it.
:We must also remember to use the new command, because using a cd will mean
:that the directory reflected in our PS1 will be incorrect.
:
:You can try this Bourne shell function :
:
:	go()
:	   {
:	   if [ "$1" ]		# if we got an argument
:	   then
:	      cd $1
:	   else 		# otherwise go home
:	      cd $HOME
:	   fi
:	   PS1=`pwd`"> "	# change the PS1
:	   }
:

For what it's worth,  here is how I implemented things (from
file /usr/acct/ME/bin/.functs which I invoke as . .functs in
my .profile)

#
#       This file invokes function definitions
#
# ps3="`cat /etc/NODENAME`/`basename \`tty\``> "  # set in .profile
pd() { RT=$PWD; cd $1; PWD=`pwd`; PS1="$PWD
${ps3}"; }
lpd()	{ pd $1; ls; }
rt() { pd $RT; }

pd (path directory) changes my directory and reflects the current
directory as the first line of my two line prompt.  The second line
tells me what system I am on (I have access to several) and what
line (I also have a variety of ways to tie in - modem,  direct line,
pass thru line,  Token Ring).  If I am in a sub shell,  a /n is 
appended to the line number to indicate depth,  since I'm frequently
nested through some intermediary program or other.

I can toggle between two directories with the rt command,  very
useful wehn transferring files (mv pattern $RT).

:Remember, shell functions are not passed to their subshells.
:
But are accessible by shell scripts called by current shell.  If
you are lucky enough to have an NCR Tower (no flames please,  they
are my employers) they have the delightful .shrc feature implemented
that gets invoked when you fork an interactive shell.  Once I found
that gem (by accident - it's not documented)  I was able to invoke
my function definitions on all shell levels.

I have lots of other shifty functions.  vi (no args) for instance,
edits my last edited file rather than a file with no name.  Very
useful when mucking with balky items like makefiles.


:Chris Hare				Choreo Systems Inc.
:Coordinator, Systems Management		150 Laurier Ave West
:Authorized SCO Instructor		Ottawa, Canada
:					Phone 613-238-1050
:					Fax   613-238-4453
:*** It Works for ME ! ***		email : uunet!choreo!chris


-- 

Dan Mercer
Reply-To: mercer@ncrcce.StPaul.NCR.COM (Dan Mercer)