How to display current time in the prompt


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to display current time in the prompt
# 29  
Old 10-03-2012
Quote:
Originally Posted by rdrtx1
manishdivs, since your shell supports Chubler_XL's solution, use it. Forget about trapping SIGs. I'm stuck until the system is changed.
Ok.. Thanks a lot for your help and time for solving my query.
# 30  
Old 10-03-2012
On my linux box, ksh:
Code:
PS1='$(date)'" > "

works fine:
Code:
Wed Oct  3 18:50:28 CDT 2012 >
Wed Oct  3 18:50:29 CDT 2012 >
Wed Oct  3 18:50:29 CDT 2012 >
Wed Oct  3 18:50:29 CDT 2012 >
Wed Oct  3 18:50:30 CDT 2012 >
Wed Oct  3 18:50:30 CDT 2012 >
Wed Oct  3 18:50:31 CDT 2012 >
Wed Oct  3 18:50:31 CDT 2012 >
Wed Oct  3 18:50:31 CDT 2012 >

Code:
echo ${.sh.version}

Version JM 93t+ 2010-06-21

Last edited by rdrtx1; 10-03-2012 at 09:01 PM.. Reason: Include ksh version
# 31  
Old 10-03-2012
Quote:
Originally Posted by manishdivs
Worked fine in ksh.

Thanks a lot for the solution. By any chance is there any other way (simpler) also do achieve this?
This is a fairly good solution as it dosn't run the date executable everytime you hit enter, all the calculations are done in pure ksh.

The trap solution (if you get it working) will continue to display the same time until you another external command, also if you change PS1 it will change it back on you:

Code:
$ trap 'PS1="$(date +%H:%M:%S) $"' SIGCHLD
$ echo
$ /bin/echo "now"
now
09:54:21 $PS1='# '
# 
# echo
# /bin/echo "oops"
oops
09:54:35 $

@rdrtxt Linux typically uses pdksh which supports the construct you posted.
# 32  
Old 10-04-2012
Quote:
Originally Posted by Chubler_XL
Try this: in file setprompt put:

Code:
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="$_timehms $ "


Then from your command line, source the setprompt file eg:

Code:
 $ . ./setprompt
09:25:27 $ 
09:25:38 $


Hello Chubler_XL,

This code worked perfectly for me but I have a silly question here.

If I run this code as
Code:
. ./setprompt

then it works fine but if I run the script like
Code:
./setprompt

then it do nothing.

What is the difference between these two ways to execute the script?
# 33  
Old 10-04-2012
This is called sourcing and the shell reads the commands from the source file and executes them in the current shell.

When you use ./setprompt the current interactive shell spawns a new child shell and the script is run by that. PS1 is changed in the child shell correctly, but then this child shell terminates returning control back to your current interactive shell with your old PS1 value.
This User Gave Thanks to Chubler_XL For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Prompt path display issue

I use the following command to print the current directory above the command prompt set prompt="`exec pwd`\n$USER@`hostname -s` %B: % > " The output is something like this <current path> $USER@hostname > But when I try to CD to any other directory and press the return key, the... (6 Replies)
Discussion started by: aelhosiny
6 Replies

2. Shell Programming and Scripting

Convert UTC time into current UNIX sever time zone

Hi guys thanks for the help for my previous posts.Now i have a requirement that i download a XMl file which has UTC time stamp.I need to convert UTC time into Unix server timezone. For ex if the time zone of unix server is CDT then i need to convert into CDT.whatever may be the system time... (5 Replies)
Discussion started by: mohanalakshmi
5 Replies

3. Shell Programming and Scripting

Displaying current date time of EDT in IST time

Hi Folks, My server time is in EDT. And i am sending automated mails from that server in which i need to display the current date time as per IST (GMT+5:30). Please advice how to display the date time as per IST. IST time leads 9:30 mins to EDT. and i wrote something like below. ... (6 Replies)
Discussion started by: Showdown
6 Replies

4. HP-UX

Unable to Set Prompt to current working DIR

HPUX does not recognise \h,\w,\u to display the hostname,working directory and username respectively. So how do i set the PS1 variable to display my current working Directory as my prompt? I also tried PS1=$PWD, But it keeps showing the same directory path as prompt which PWD was holding at... (3 Replies)
Discussion started by: Amit Kulkarni
3 Replies

5. UNIX for Dummies Questions & Answers

Displaying the current working directory in prompt

Hi, I want that the prompt that is being displayed (i.e $ sign) should display always the current directory I am working in instead of that $ sign example: as we use PS1=patric and the prompt changes from $ to patric OR if we write the command PS1=`pwd` it will display the current... (5 Replies)
Discussion started by: premjotsingh
5 Replies

6. Shell Programming and Scripting

Current working directory in prompt

Can someone tell me how do i set my current working directory in my prompt? Note: I dont want to use env variable PWD and using `pwd` gives me only my home directoy. suggest a different way ? I use /bin/sh thanks!! (3 Replies)
Discussion started by: yesmani
3 Replies

7. Shell Programming and Scripting

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... (4 Replies)
Discussion started by: m223464
4 Replies

8. UNIX for Dummies Questions & Answers

Getting current work directory in Command Prompt

How to get the current working directory as part of the command prompt? Every time I chage the folder, my command prompt path shoud change. I am using Korn Shell. Any help is greatly appreciated. (3 Replies)
Discussion started by: MeganP
3 Replies

9. Solaris

no boot prompt display

just put blank harddisk in my ultra10. i see no display to show me the boot> prompt. i don't know what happened....?? Got a third party graphic card. Display was ok. Monitor seems to be ok. i used different one with same result. (1 Reply)
Discussion started by: S26+
1 Replies

10. Shell Programming and Scripting

current directory as part of the csh prompt

I would like my csh prompt to behave like the linux csh prompt setting done by linux command (set prompt="%n@%m %c]$ ") how do I do that? What I'm trying to do is that I would like to see what directory I'm in by looking at the prompt. I've figured out that %n is like $user, and %m is like... (3 Replies)
Discussion started by: jamesloh
3 Replies
Login or Register to Ask a Question