[comp.unix.questions] Prompt changing on the fly

wyle@ethz.UUCP (01/16/87)

Does anyone out there have a shell script / program to change
the prompt DYNAMICALLY after each [CR]?

I would like to choose a random prompt from a large file, and
I would like the flexibility to add: node, directory, etc. to
the generated prompt.

I would prefer NOT to write a new shell.

Ideas?
---

-- 
                      M i t c h e l l     F     W y l e

    EEEEE TTTTTT H   H  Eidgenoessische      | wyle%ifi.ethz.chunet@relay.cs.net
   E       T    H   H  Technische Hochschule | wyle@ethz.uucp
  EEEE    T    HHHHH  Zuerich                | ...!cernvax!ethz!wyle
 E       T    H   H  Institut fuer Informatik| Telephone: 011 41 1 256 5235 
EEEEE   T    H   H  8092 Zuerich, Switzerland| "Ignore alien orders"

lvc@danews.UUCP (01/18/87)

What shell are you using?  It is possible in the Korn shell.
Impossible in the Bourne shell.  I don't know about the C-shell.
My prompt includes a clock that is updated at every keystroke.
-- 

Larry Cipriani   Cornet 353-4999 AT&T (614) 860-4999
{ihnp4|cbosgd}!cbsck!lvc   AT&T Network Systems rm 2B-220

cdash@boulder.UUCP (Charles Shub) (01/20/87)

In article <432@ethz.UUCP> wyle@ethz.UUCP (Mitchell Wyle) writes:
>Does anyone out there have a shell script / program to change
>the prompt DYNAMICALLY after each [CR]?
>
it is simple to define a command/alias to set your prompt and then redefine
commands to also set your prompt

alias cd 'chdir \!*;SP'
alias SP 'set prompt="$MACHINE-`basename $cwd` \! > "'

then the prompt does what you want

storm@diku.UUCP (01/23/87)

In article <432@ethz.UUCP> wyle@ethz.UUCP (Mitchell Wyle) writes:
>Does anyone out there have a shell script / program to change
>the prompt DYNAMICALLY after each [CR]?

You cannot use a shell script or a program to change the shell prompt
dynamically, because the PS1/prompt variables are local to the shell,
and therefore they can only be changed by the shell itself.  

I don't know of any hooks into the Bourne/C shells which enables you to
recognize that CR has been typed, and thus not to execute any commands
in connection with execution of other commands.

BUT you should try out the following 'trick' which gives some of
the functionality you want:

Include the following lines in your .profile (this is Bourne Shell)
or type them in directly if you like:

	trap 'PS1="`fortune` > "' 5
	while true ; do
		sleep 10 ; kill -5 $$
	done &

Now watch what happens after the execution of the next command.

This is not exactly what you asked for, but the idea might be of some
use to you.

--
Kim F. Storm, storm@diku.UUCP (via seismo or mcvax or ...)
Institute of Datalogy(=CS), U of Copenhagen, Universitetsparken 1, DK-2100 OE

serge@imag.UUCP (02/02/87)

In article <2935@diku.UUCP> storm@diku.UUCP writes:
>You cannot use a shell script or a program to change the shell prompt
>dynamically, because the PS1/prompt variables are local to the shell,
>and therefore they can only be changed by the shell itself.  

avec la commande  chdir :    echo directory ?
                             read  dir
                             cd $dir && PS1=`pwd`" $ "

et l'appel de commande  :    . chdir
-- 
================================================================================
serge@imag.UUCP   or {seismo.css.gov|mcvax|inria}!imag!serge
Rouveyrol Serge,Laboratoire L.G.I,BP 68,38402 St Martin d'Heres CEDEX,FRANCE
tel 76 51 46 00 poste 5132

wcs@ho95e.UUCP (02/03/87)

In article <2935@diku.UUCP> storm@diku.UUCP writes:
>In article <432@ethz.UUCP> wyle@ethz.UUCP (Mitchell Wyle) writes:
>>Does anyone out there have a shell script / program to change
>>the prompt DYNAMICALLY after each [CR]?
>
>You cannot use a shell script or a program to change the shell prompt
>dynamically, because the PS1/prompt variables are local to the shell,
>and therefore they can only be changed by the shell itself.  

If all you want to do is set the prompt based on a few variables (process id,
current directory, some-shell-variable, history-number), ksh and csh have some
hooks to do these.  I don't know csh, but I've seen people whose prompt
includes a number incremented by 1 each command; makes it easier to use csh's
boring version of history.

Ksh (which is upward-compatible with sh, and has decent history), allows you to
evaluate variables each time you access PS1.  Thus, my PS1 is set to
	PS1='e!!${PWD}> '
which looks like
	e!/usr/spool/news>
(the ! is doubled to prevent csh-like history-number substitution)
I don't think you can do execution at the prompt (which would be really slow),
but the newer versions of ksh let you set SECONDS to get a crude clock.
	SECONDS=0; export SECONDS
	PS1='Logged in $SECONDS: '
You can also set MAILPATH to check files other than just $MAIL.
-- 
# Bill Stewart, AT&T Bell Labs 2G-202, Holmdel NJ 1-201-949-0705 ihnp4!ho95c!wcs