Operating System - HP-UX
1827807 Members
2353 Online
109969 Solutions
New Discussion

Re: PROBLEM WITH PROMPT AND HOUR

 
SOLVED
Go to solution
gaudiobe
Advisor

PROBLEM WITH PROMPT AND HOUR

I want to put the current hour on my unix prompt and I found the right script in order to do it. And I've added it in my .profile file.

=====================================
typeset -RZ2 _x1 _x2 _x3
let SECONDS=$(date '+3600*%H+60*%M+%S')
_s='(_x1=(SECONDS/3600)%24)==(_x2=(SECONDS/60)%60)==(_x3=SECONDS%60)'
TIME='"${_d[_s]}$_x1:$_x2:$_x3"'
export PS1=`hostname`"[$TIME]:\$PWD> "
=====================================

The problem was the following:
If I logging in with my personal user account it work and the prompt was displayed correctly.
If logging in with root account I wasn't able to have the right prompt.
EXAMPLES:
For my ACCOUNT
myhost[10:36:41]:/home/myuser>
For root ACCOUNT:
myhost["${_d[_s]}$_x1:$_x2:$_x3"]:$PWD>

P.S.: Both the user are useing the same SHELL: /sbin/sh

Thanks for any help.
12 REPLIES 12
Muthukumar_5
Honored Contributor

Re: PROBLEM WITH PROMPT AND HOUR

Prompt settings are using PS1 variable. You can set PS1 setting in /etc/profile or $HOME/.profile file to read that value.

hth.
Easy to suggest when don't know about the problem!
gaudiobe
Advisor

Re: PROBLEM WITH PROMPT AND HOUR

Thanks for the reply but it didn't solve the problem I've already tried it.
Eric Antunes
Honored Contributor

Re: PROBLEM WITH PROMPT AND HOUR

Hi Bernardo,

You must quote all like this:

export PS1="`hostname`# ..."

Or else, you are changing the hostname...

Best Regards,

Eric Antunes
Each and every day is a good day to learn.
Muthukumar_5
Honored Contributor

Re: PROBLEM WITH PROMPT AND HOUR

Did you try with /etc/profile file?? It will be execute for every user who is using terminal based login (not CDE).

/etc/profile
PS1=`$(hostname)[(date +'%H:%M:%S')]:$PWD>'
export $PS1

hth.

Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: PROBLEM WITH PROMPT AND HOUR

It is as,

PS1='$(hostname)[(date +'%H:%M:%S')]:$PWD>'

` came instead of '.
Easy to suggest when don't know about the problem!
Suraj Singh_1
Trusted Contributor

Re: PROBLEM WITH PROMPT AND HOUR

Try:

export PS1=`hostname`[`date +%H:%M:%S`]:\$PWD

Regards
What we cannot speak about we must pass over in silence.
Muthukumar_5
Honored Contributor

Re: PROBLEM WITH PROMPT AND HOUR

Did you get answer? IS it working?
Easy to suggest when don't know about the problem!
gaudiobe
Advisor

Re: PROBLEM WITH PROMPT AND HOUR

Thanks to all. But:

For Eric:
I've quoted everything but it didn0t works.

For Muthukumar:
I tried to put the script in the /etc/profile but it work for my user account and not for root account.
The prompt that you have suggested me didn't update the date every time that you execute a command and the prompt come back.

My prompt should be like this:
myhost[10:10:10]/home/myuser> cd ..
myhost[10:10:14]/home>cd myuser
myhost[10:10:19]/home/myuser>
....

And not:
myhost[10:10:10]/home/myuser> cd ..
myhost[10:10:10]/home> cd myuser
myhost[10:10:10]/home/myuser>

For suraj:
This is not an export problem I know that I've to export the PS1 varable.

Regards,
Bernardo
Eric Antunes
Honored Contributor

Re: PROBLEM WITH PROMPT AND HOUR

Hi,

You only get "10:10:10" because PS1 in ONLY seted at login. There a function you may use for thids: the clock function (man clock)...

Regards,

Eric Antunes
Each and every day is a good day to learn.
Muthukumar_5
Honored Contributor

Re: PROBLEM WITH PROMPT AND HOUR

PS1 varialble can show date, user, hostname and pwd. These details are static to that moment. Time will be variable.

$PWD will be update when directory is navigated. So that it will be replicated in prompt.

If you export it with date command, it will keep static data of time. I think there may be a change to have this in bash PS1.

check this out,
http://www.nersc.no/~knutal/unix_tips.html
Easy to suggest when don't know about the problem!
CAS_2
Valued Contributor
Solution

Re: PROBLEM WITH PROMPT AND HOUR

Bernardo, try the following:

set +u

I don't know the internals of this shell flag.

In my box:

["${_d[_s]}$_x1:$_x2:$_x3"]> set +u
[14:03:47]>

I remember there are 'set +u' and 'set -u' in the root's .profile.
gaudiobe
Advisor

Re: PROBLEM WITH PROMPT AND HOUR

Great CAS!!!
BINGO!!!
set +u works fine!

Thanks to all.
Bernardo