[PLUG] Putting Date/Time in ksh prompt

Steve Bonds 1s7k8uhcd001 at sneakemail.com
Thu Sep 26 00:02:35 UTC 2002


Sandy/Tyler:

Thanks for the great links.  My refined google search on "ksh PS1 prompt
date time" returned lots of good info.  Amazing how well one can refine a
search AFTER a good page is found.  ;-)

Bash is great-- "\d \t" and you're done.  Unfortunately, bash isn't a
standard install on most commercial UNIX installs... yet.

  -- Steve

On Wed, 25 Sep 2002, Tyler F. Creelan creelan at engr.orst.edu XXXXXXXXXXXXXXXXXXX wrote:

> On Wed, 25 Sep 2002, Steve Bonds wrote:
> > Here's one of those random questions that's been puzzling me for a long
> > time-- is it possible to embed the date/time in a ksh shell prompt?
> 
> I think the method you described is correct, it just looks like a
> bit of hackery is required. Here is one method:
> 
> "How to make your Unix prompt show the time"
> http://www.clearnight.com.au/cnsuxtp.htm
> 
> and here is code with an explanation of how it works:
> 
> "ksh time prompt PS1; It works, but *why*?"
> http://bizforums.itrc.hp.com/cm/QuestionAnswer/1,,0x8e134b3ef09fd611abdb0090277a778c,00.html
> 
> One of the articles looked temporary, so the info is appended below for
> PLUG archives.
> 
> Btw, in bash such hackery is not required. The @ symbol in PS1 yields the
> time, or alternately PROMPT_COMMAND can be used to call date each time
> PS1 is evaluated. This makes your date dynamic as well as time.
> 
> 
> 
> -----------------------------
> Tyler F. Creelan
> College of Engineering
> Oregon State University
> 503-640-3101
> -----------------------------
> 
> On Wed, 25 Sep 2002, Steve Bonds wrote:
> 
> > Here's one of those random questions that's been puzzling me for a long
> > time-- is it possible to embed the date/time in a ksh shell prompt?
> >
> > I keep logs of most of my shell sessions.  It would be helpful to have
> > each prompt time/date stamped so that later I can correlate what I was
> > doing with what broke at the time.  ;-)
> >
> > It looks like the closest I can do is to embed an arbitrary shell variable
> > which will be evaluated each time the prompt comes up.  Fortunately, ksh
> > includes the handy variable $SECONDS which evaluates to the number of
> > seconds since login.  If I do a "date" each time I log in and include the
> > number of seconds in the propmt, then it becomes possible to extrapolate
> > from that start time to get the date/time.
> >
> > However, I'm hoping there's a better way.  Anyone know?
> >
> >   -- Steve
> >
> 
> -----------------------------------------------------------
> http://www.clearnight.com.au/cnsuxtp.htm
> 
> How to make your Unix prompt show the time
> 
> This script is designed for ksh, and relies on the fact that Unix
> increments a variable called SECONDS every second.
> 
> If you don't change the value of SECONDS, it contains the number of
> seconds since you logged in.
> 
> Here is the script, which we can call "timeprompt". It inserts the time
> into your existing ksh prompt.
> # Include time in prompt.
> export SECONDS="$(date '+3600*%H+60*%M+%S')"
> typeset -Z2 _h; typeset -Z2 _m ; typeset -Z2 _s    # 2 digits, zero padded
> # hours, minutes and seconds...
> _hh="(SECONDS/3600)%24"
> _mm="(SECONDS/60)%60"
> _ss="(SECONDS)%60"
> _time='${_x[(_m=_mm)==(_h=_hh)==(_s=_ss)]}$_h:$_m:$_s'
> export PS1=$(echo "${_time}\n${PS1}")
> 
> Place timeprompt somewhere in your PATH.
> Modify your .profile and insert a line after your PATH and PS1 have been
> set. Use this command: ". timeprompt"
> This version of timeprompt begins by setting SECONDS equal to the time
> (the number of seconds since midnight).
> It then creates three variables that will always contain two-digits and be
> zero-padded.
> The _time variable is set to a formula that uses the evaluation of
> subscripts in a dummy array _x to format the time.
> The last line of timeprompt inserts _time , and a newline character into
> your prompt.
> Replace \n with a space if you want a single line prompt, but this may
> become too long if you also include values such as ${HOSTNAME}, ${LOGNAME}
> and ${PWD}
> 
> Timeprompt has been tested on AIX and UnixWare. It may require some
> modification to work with ksh on other versions of Unix.
> This information is subject to review and improvement. If you have any
> questions or suggestions, please email us at Clear Night Software or use
> our Feedback Form.
> 
> 
> 
> -----------------------------------------------------------
> http://bizforums.itrc.hp.com/cm/QuestionAnswer/1,,0x8e134b3ef09fd611abdb0090277a778c,00.html
> 
> ksh time prompt PS1; It works, but *why*? (ksh obfuscation?)
> 
> I've been looking at a current, continuously updated time prompt in ksh,
> similar to:
> 
> http://forums.itrc.hp.com/cm/QuestionAnswer/1,,0xa0ead211e18ad5118ff10090279cd0f9,00.html
> 
> The variations on this item in my config are below. I see that it works. I
> see that it has bugs (i.e., 17:10:00 fails). I'd like to understand what
> is happening. Some things I get, others I do not:
> 
> 1) I do not understand where _d comes from in the example above. Does it
> matter?
> 2) I understand the typeset part; it assures the width of the
> hour/minute/seconds variables.
> 3) I do not understand why let is used in one place (let SECONDS), but not
> in the other (s=).
> 4) I do not understand the == in the SECONDS assignment.
> 5) Why are there no $ (dollar-signs) in the s variable assignment.
> 6) The [s] array reference totally blows my mind. What is this really?
> 7) I understand that a single-quote item will not be evaluated until run
> time.
> 
> One item at a time, please. I'm squinitng like I'm trying to straighten a
> corkscrew using only telekenses.
> 
> ----- Begin script:
> 
> # Make certain that the length of time fields is 2-digit:
> typeset -RZ2 hr min sec
> 
> # Get a string which is parsable to the number of seconds.
> let SECONDS=$(/usr/bin/date '+3600*%H+60*%M+%S')
> 
> s='(hr=(SECONDS/3600)%24)==(min=(SECONDS/60)%60)==(sec=SECONDS%60)'
> d=
> TIME='"${d[s]}$hr:$min:$sec"'
> export PS1=${TIME}':'
> Note: If you are the author of this question and wish to assign points to
> any of the answers, please login first. Click here for more information on
> assigning points.
> Sort Answers By: Date or Points
> David Totsch
> 
> 
> July 27, 2002 03:08 AM GMT   [ 10 pts ]
> I am suprised that all of the Olympians are not answering this one...could
> it be that they actually go home once in awhile, or are they taking
> National System Administrator appreciation day off...
> 
> ${_d[_s]} is "slight-of-hand"...you have to trick the shell into making
> the evaluation set-up in that s='...' line. More about that later.
> 
> The 'let' makes sure the string returned by date(1) is evaluated to a
> number (sorta like eval would make a second pass on the same line), but
> let does arithmetic stuff.
> 
> ME=5+5
> 
> leaves ME containing the string "5+5"
> 
> let ME=5+5
> 
> leave ME containing the string "10".
> 
> Since the Bourne shell, we have gotten into the habit of evaluating
> arithmetic statements in other ways.
> 
> SECONDS is an automatic shell variable that contains the number of seconds
> since shell invocation. If you try to set SECONDS to a number, like the
> let in our example, SECONDS contains the number you set it to PLUS the
> number of seconds since shell invocation. Hence, SECONDS keeps the current
> time in seconds.
> 
> The output is "two" (stuff between square brackets is evaluated). And _s
> is a big, long, boolean equation that sets the variables "hr", "min",
> "sec" using the current value of SECONDS (yup, without the $). You can do
> the same with $((...)) or ((..)).
> 
> You could say that _d stands for "dummy" (we just needed a dummy array)
> and _s stands for "seconds string".
> 
> When the shell tries to print ${_d[_s]}, it evaluates _s arithmetically.
> Breaking down _s, hr=(SECONDS/3600)%24 is an arithmetic statement that
> sets hr to a number. It is also a boolean statement that is always true.
> Taking out the code that sets variables, you have
> 
> (...)==(...)==(...)
> 
> The '=='s are boolean statements testing for equality. They don't "do"
> anything but let one statement set all three variables.
> 
> To get a feel for how this works, consider the following code:
> 
> a[0]=zero
> a[1]=one
> a[2]=two
> a[3]=three
> a[4]=four
> 
> echo ${a[1+1]}
> 
> 
> Hope that helps,
> -dlt-
> 
> 
> 
> _______________________________________________
> PLUG mailing list
> PLUG at lists.pdxlinux.org
> http://lists.pdxlinux.org/mailman/listinfo/plug
> 






More information about the PLUG mailing list