Operating System - HP-UX
1819875 Members
2990 Online
109607 Solutions
New Discussion юеВ

posix shell and PS1 values

 
SOLVED
Go to solution
Mauro Gatti
Valued Contributor

posix shell and PS1 values

Hi all, Do You know where I can find "codes" to assign to PS1 that stant for hostname, current directory, time etc. etc.

Thank You
Ubi maior, minor cessat!
8 REPLIES 8
Jeroen Peereboom
Honored Contributor

Re: posix shell and PS1 values

Well,

I know how to assign the hostname and current directory:
export PS1="`hostname`"'$PWD> '

It is important that $PWD is surrounded by ' and not ".

JP
Karthik S S
Honored Contributor

Re: posix shell and PS1 values

PS1=root@`hostname`:'$PWD''>'
export PS1

-Karthik S S
For a list of all the ways technology has failed to improve the quality of life, please press three. - Alice Kahn
Vijaya Kumar_3
Respected Contributor

Re: posix shell and PS1 values

I hope it is not in "man sh". But i found a detailed discussion about PS1 in UNIX poer tools Book from Oreilly - Chapter 4

http://www.oreilly.com/catalog/upt3/
Known is a drop, unknown is ocean - visit me at http://vijay.theunixplace.com
Jean-Luc Oudart
Honored Contributor

Re: posix shell and PS1 values

you can set it up in /etc/profile

PS1=':$LOGNAME:${PWD} $PROMPT '
export PS1

Regards,
Jean-Luc
fiat lux
Mauro Gatti
Valued Contributor

Re: posix shell and PS1 values

Thank You guys... I'm finding a complete list that I can use in HP-UX "sh" of these "parameters" like reported in "bash" man pages for example.
Does it exist?

Thank You
Ubi maior, minor cessat!
Steve Steel
Honored Contributor

Re: posix shell and PS1 values

Hi

Example with vis to show characters

Line numbers to help extraction



1 #!/usr/bin/ksh
2 # Make certain that the length of time fields is 2-digit:
3 #set -x
4 typeset -RZ2 hr min sec
5
6 # Get a string which is parsable to the number of seconds.
7 let SECONDS=$(/usr/bin/date '+3600*%H+60*%M+%S')
8
9 s='(hr=(SECONDS/3600)%24)==(min=(SECONDS/60)%60)==(sec=SECONDS%60)'
10 d=
11 TIME='"${d[s]}$hr:$min:$sec"'
12 #export MESSAGE=\\${TIME}':'"On "\\$PWD" "\\$MESSAGE
13 #echo $TIME $MESSAGE
14 echo "\e&v4S $LOGNAME \e&v5S $(uname -nmrv) \e&v2S:\e&v4S $MESSAGE
15 \e&v5S\\$PWD\e&v1S >\e&v0S


Steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
Jeroen Peereboom
Honored Contributor

Re: posix shell and PS1 values

Mauro,

bash has more 'built-in' variables for the prompt than ksh or sh-posix. I can remember reading an article on ksh, which listed the code for a time in the PS1 pronpt. I didn't understand it. I think the next url refers to that article:
http://www.itworld.com/AppDev/1458/swol-0316-regex/

Quote:
Portable shell
ksh93 is freely available for essentially all Unixes, and also, with light license restrictions, for Windows and MVS. A handful of vendors support ksh93 commercially. Korn's team at AT&T plans to upgrade a future release of KornShell with a combination of object inheritance, support for binary objects, and namespaces. Also for that release, Korn writes, "Multithreading is a possibility, but less likely than the others."

Korn has even made much of the contents of his authoritative reference book on ksh available for download. Here's an example fragment, which gives an idea of ksh's syntax and functionality:


# Set SECONDS to number of seconds since midnight.
export SECONDS="$(date '+3600*%H+60*%M+%S')"
# The following variables store hours and minutes.
typeset -Z2 _h _m # Two columns, leading zeros.
# The following expression reformats SECONDS.
_time='$((_h=(SECONDS/3600)%24)):$((_m=(SECONDS/60)%60))'
# Use _time within PS1 to get the time of day.
PS1="($_time)"'!$ '
# Note that $_time gets replaced by above expression.
# Expression gets evaluated when PS1 is displayed.


This example is particularly interesting as an example of language evolution. In 1999, after the book was published, ksh's printf operation was augmented to include new time capabilities. A consequence is that all the code above can now be abbreviated to the single line:


PS1='$(printf "(%(%H:%M)T)!$ " now)'


End Quote.

Looks similar to Steve's reply.

Hope this helps,

JP.

Geoff Wild
Honored Contributor
Solution

Re: posix shell and PS1 values

DIdn't really find much on this...but....you can put in PS1 just about anything you like.

Setting and Referencing Variables:

http://docs.hp.com/cgi-bin/fsearch/framedisplay?top=/hpux/onlinedocs/B2355-90164/B2355-90164_top.html&con=/hpux/onlinedocs/B2355-90164/00/00/41-con.html&toc=/hpux/onlinedocs/B2355-90164/00/00/41-toc.html&searchterms=PS1&queryid=20040210-064956

This is what I do:

HOST=`uname -n`
PS1="\$HOST:\$LOGNAME:\$PWD # ";export PS1


There's another example here:
http://www.nersc.no/~knutal/unix_tips.html


Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.