The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Getting current work directory in Command Prompt MeganP UNIX for Dummies Questions & Answers 3 07-20-2007 10:49 AM
Adding # minutes to current time... gptavares UNIX for Advanced & Expert Users 7 06-25-2007 12:10 PM
Setting the current path in the command prompt in (t)csh reborg Answers to Frequently Asked Questions 0 10-10-2006 12:02 PM
Substituting variable with current time jhansrod Shell Programming and Scripting 4 11-20-2005 04:20 PM
current directory as part of the csh prompt jamesloh Shell Programming and Scripting 3 08-27-2002 10:38 PM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-28-2005
Registered User
 

Join Date: Mar 2005
Posts: 39
Help setting PS1 prompt to include current time

Hi,

I'm using the ksh shell and I'd like to set my PS1 prompt on an AIX system to include, amongst ther things, the current time.

This was my best effort: export PS1=$(date -u +%R)'${ME}:${PWD}# '
but this only sets the time to the value when PS1 is defined and the time value doesn't actually change after that.

Any one got any ideas how to achieve this? I've tried searching the forum but can't find anything.

Thanks

Gareth
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 11-28-2005
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 3,491
PWD, RANDOM and some other environment variables are defined by the shell to point to a function. I don't know of a function defined as a shell symbol that returns date & time.

In the korn shell try:
Code:
PS1='$RANDOM'
Then press return five or six times. There has to be code behind the call to the environment variable becuase it is dynamic.
Reply With Quote
  #3 (permalink)  
Old 11-28-2005
Ygor's Avatar
Moderator
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,226
Try quoting the whole string: export PS1='$(date -u +%R) ${ME}:${PWD}# '
Reply With Quote
  #4 (permalink)  
Old 11-28-2005
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,354
Ygor's solution will work with ksh95 or pdksh. But ksh88 only expands variables...it won't do a general parse. I am not real fond of invoking the date command with each each display of the prompt.

ksh95 has a feature called discipline functions and these can be used to create dynamic variables that work like RANDOM.

Here is a solution that will work with all three versions of ksh. If we set SECONDS, it will continue to advance. So if we set it to the number of seconds since midnight it will serve as a time-of-day clock. To get from seconds-after-midnight to hours:minutes:seconds we need to do some arithmetic. If we reference a imaginary array, we can compute a subscript for it and use side effects from the subscript computation to set other variables. A single date process is used to initialize the clock, but after that the time is computed by the shell itself.

unset _h _m _s
eval $(date "+_h=%H ;_m=%M ;_s=%S")
((SECONDS = 3600*${_h#0}+60*${_m#0}+${_s#0}))
typeset -Z2 _h _m _s
_tsub="(_m=(SECONDS/60%60)) == (_h=(SECONDS/3600%24)) + (_s=(SECONDS%60))"
_timehm='${_x[_tsub]}$_h:${_m}'
_timehms='${_x[_tsub]}$_h:$_m:${_s}'

PS1="$_timehm $ "
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 09:03 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0