Operating System - HP-UX
1825737 Members
2584 Online
109687 Solutions
New Discussion

Re: got time working in shell prompt, but date is not

 
digger86
Occasional Advisor

got time working in shell prompt, but date is not

I found on the forum how to get time to work in a shell prompt.

#cat ~/.profile

~sniped~
export DATE="$(date +'%D')"
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'
~sniped~

but the date does not change.

11 REPLIES 11
Patrick Wallek
Honored Contributor

Re: got time working in shell prompt, but date is not

I am confused. What exactly are you trying to accomplish? What's not working? What does the output look like?
Steven E. Protter
Exalted Contributor

Re: got time working in shell prompt, but date is not

Shalom,

If you are trying to make the prompt include the date, which I think is a bad idea, set the values you want into the variable PS1

Your snipped code area needs to include perhaps the date command.

variable=$(date)

Do some further processing in the parenthesis, to get the exact data you want.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
digger86
Occasional Advisor

Re: got time working in shell prompt, but date is not

Patrick Thanks for trying to help

# cat ~/.profile
# Custom varibles
EDITOR=vi
export EDITOR
stty erase ^?
export HISTFILE=$HOME/.sh_history
export HISTSIZE=5000
export HISTFILE HISTSIZE
alias ls="ls -aF"
export DATE="$(date +'%D')"
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="^[[35m$(echo ${DATE} ${_time}) ^[[32m`whoami`@^[[31m`hostname`^[[36m
\$PWD ^[[0m$(echo "\n# ")"

My prompt look like:
06/26/10 11:32:04 username@host /home/username
#

The date does not change but time does.

Yes, this is very Linux like :)
digger86
Occasional Advisor

Re: got time working in shell prompt, but date is not

Steven, why do you think this is a bad idea?
Dennis Handly
Acclaimed Contributor

Re: got time working in shell prompt, but date is not

>why do you think this is a bad idea?

Because the prompt should only be "$ " and nothing else.
It is the window title bar that should have the machine and directory path.
And you should have one clock on your desktop, not timestamps for each of your shells.
Patrick Wallek
Honored Contributor

Re: got time working in shell prompt, but date is not

>>Because the prompt should only be "$ "

Say what? Most things I agree with you, Dennis, but that is one of the most asinine statements I've ever read.

If he wants the date and time on the prompt, more power to him.

>>It is the window title bar that should have
>>the machine and directory path.

Well, I can't say I agree there either.

Part of the beauty of Unix is you can make it do almost anything you want. I personally prefer the server name and pwd to be in my PS1 prompt.

Everyone is entitled to their own opinion, I just happen to completely disagree with you in this case.
Dennis Handly
Acclaimed Contributor

Re: got time working in shell prompt, but date is not

>Patrick: I personally prefer the server name and pwd to be in my PS1 prompt.

It depends on what you are doing.
If you absolutely positively need to know what machine you are on, I see your point.

But I prefer a minimalist prompt so I can see my commands, especially when command editing.
I found that prompt fanciness gets in the way.

>I just happen to completely disagree with you in this case.

That's fine. :-)
digger86
Occasional Advisor

Re: got time working in shell prompt, but date is not

Yes, I need to see the host name. And I clearly separate between commands by making my prompt very colorful (Linux does this for that reason). Blue hostname means non-production host. Red means production.

I am using escape sequences to add color to my prompt.

Having time it great to compare things such as Service Guard fail overs quickly.

Date is great for taking snap shots of the terminal for later reference.

Elmar P. Kolkman
Honored Contributor

Re: got time working in shell prompt, but date is not

Apart from the discussion about why you want this or not (for instance, if you DON'T have a graphical environment available, or are maintaining systems world-wide), you need to provide some extra info.
For instance, what shell are you trying to use?
ksh doesn't seem to do this, so are you using bash by any chance ?
And what version of bash?

The reason it doesn't work, is because you have a very long set of lines to make the time work, but the date is pushed in a single variable and then used in the prompt. You should do something like you did with the time, to make it work.
Or when you use bash you don't have to do it so hard, just do:
PS1='$(date +"%D %H:%M:%S) \$PWD $ '
Every problem has at least one solution. Only some solutions are harder to find.
john korterman
Honored Contributor

Re: got time working in shell prompt, but date is not

Hi digger,


hour, minutes, and seconds change in your prompt because the calculation is based on a "live" variable; your shell apparently supports the special variable SECONDS, which is constantly and automatically updated with the number of seconds that the shell has been alive.
In contrast, the DATE output is based on a variable which is set only once and not updated.
Perhaps DATE can be updated by forcing re-evaluation of its arguments, e.g.:
(eval echo $(date +'%D'))
but no guarantee :-)

regards,
John K.
it would be nice if you always got a second chance
Hakki Aydin Ucar
Honored Contributor

Re: got time working in shell prompt, but date is not

>Denis: But I prefer a minimalist prompt so I can see my commands, especially when command editing.

Agree.