KSH-How do I make a variable run a function upon invocation?

Eric H. Herrin II eric at ms.uky.edu
Fri Apr 12 07:39:32 AEST 1991


jbn35564 at uxa.cso.uiuc.edu (J.B. Nicholson) writes:

>In KSH I want to make ${PS1} take on a value where it executes a function called
>PS1, but not take on the output from the function itself.  Here's what I mean:

>function PS1 {
>shrunkhome=${PWD%${HOME}/}
>case ${shrunkhome} in
>        ${HOME})        shrunkhome=\~/ ;;
>esac
>HOSTNAME=$(hostname)
>print -n '<'${USER}'@'${HOSTNAME%%.*}':'${shrunkhome}'> ' ;;
>}

Try this:

function _cd {
   unalias cd
   cd $1
   alias cd=_cd
   if [ "${PWD#${HOME}}" != "${PWD}" ]
   then
           PS1='"${USER}@${HOSTNAME%%.*}:~/${PWD#${HOME}}> "'
   else
           PS1='"${USER}@${HOSTNAME%%.*}:${PWD}> "'
   fi
}

and alias cd to '_cd' as you suggested in your original article.  I
did test this and it does work. (ksh version 11/16/88d)  You might
want to put some error checking in the _cd function though.



-- 

|  	Eric H. Herrin II - {rutgers,uunet}!ukma!eric, eric at ms.uky.edu	     |



More information about the Comp.unix.shell mailing list