It is currently Tue, 19 Jun 2018 21:00:14 GMT

Author Message
 ksh: Set date/time in prompt

How to do this in the current versions (e.g. of AIX 4.2 / 4.3) of the
Korn shell ?

Whenever I get a prompt, I not only want to see the current working
directory, but also the current date or at least time...

 Mon, 17 Jul 2000 03:00:00 GMT   
 ksh: Set date/time in prompt

I have the following in my .kshrc file (not the .profile as this will make the
clock incorrect every time a new shell is started):

typeset -Z2 _h _m _s
typeset -i _k
SECONDS=$(date +"%H*3600+%M*60+%S")
set -A _g
PS1='${_g[(_k=SECONDS)==(_s=_k%60)==(_m=(_k/60)%60)==(_h=(_k/3600)%24)]}$_h:$_m:$_s ${PWD} > '

(Modified from the The Korn Shell Command and Programming Language by Morris
I Bolsky and David G Korn)

 Tue, 18 Jul 2000 03:00:00 GMT   
 ksh: Set date/time in prompt

Thanks a lot (Did not know how tricky it was to get some variables calculated in PS1), but could
you explain ?
Is calculating an array index the only way to have ksh evaluate variables in PS1 every time a
command is entered ?
${_g[x]} should be an empty string, right (as it doesn't appear in the prompt) ?

- Show quoted text -

 Thu, 20 Jul 2000 03:00:00 GMT   
 ksh: Set date/time in prompt

${PWD} > '

OK, _g[x] as you say is a null string, you can try setting it and it will appear
in the prompt string, we are not interested in the contents of the array, only
the evaluation of the subscript. What we are basically doing is setting _k to
equal the number of seconds, and 'testing' to see if that is equal to the result
of the variable _s being asigned to the value of _k mod 60, and so on,. At the
end the result of the evaluation will be 0, and _g[0] will be printed, a null
string normally. In the process of the evaulation though all the variables have
been set and can be output in a formatted manner.

It's the only way I can think of, I stand to be corrected. It is not too much of
an overhead as it is all done within the shell.

Yes, you're right, as I said try setting _g[0] and see what happens


 Fri, 21 Jul 2000 03:00:00 GMT   
 ksh: Set date/time in prompt

I would write a function and place it in my .kshrc file so that all ksh
instances know about the function.

alias   -x cd=_cd                       #  always use this CD command

HN=`hostname`                           #  set the system's name
DT=`date '+%m/%d/%y'`                   #  set the date

function _cd {
    if [ $# -eq 0 ] ; then              #  No arguments passed
        set -- ~                        #  set $HOME in $1

    'cd' $@                             #  cd to command line argument
                                        #  this uses the built-in cd
    PS1="${HN} ${LOGNAME} @ ${PWD}
${DT} : ) "

#  set the prompt every time I start a shell    #
PS1="${HN} ${LOGNAME} @ ${PWD}
${DT} : ) "

By putting this information in the .kshrc.  Every new ksh will have this
prompt.  It will be a two line prompt.  The first line will have:
SystemName MyUserName @ PresentWorkingDirectory
02/08/98 : )

The second line has the date on it.  Simply modify the date command
to change the prompt to include the time.

The alias -x _cd says use the _cd command instead of the shell built-in
and the 'cd' tells the ksh to use the built-in cd command not the
alias that was exported.

Hope this helps!!
     __o         NO  FEAR  'rm: text file busy'
   _`\<,_     Bobby Higgins  email:
  (_)/ (_)        
************  peddlin' as fast as I can

 Thu, 27 Jul 2000 03:00:00 GMT   
   [ 5 post ] 

Similar Threads

1. KSH date and time in prompt

2. setting date/time in system prompt

3. ksh and dates in prompt...

4. reset system time/date from prompt?

5. Korn Shell Time/Day/Date Prompt (REPOST/FIX)

6. Displaying time in PS1 prompt (ksh on HPUX 9.01 or 10.0)

7. Displaying time in PS1 prompt (ksh on HPUX 9.01 or 10.0)

8. Getting the time in prompt for KSH

9. Q:Current time as part of ksh prompt?

10. using time as prompt with ksh

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software