If all you want is a big fat fixed warning, then simply add it to the prompt:
red=$(tput setaf 1; tput smso; tput bold) reset_color=$(tput sgr0)
PS1='
${red}[THIS IS A PRODUCTION SYSTEM!!]${reset_color}
rest of the prompt$ '
In modern (post 1995) implementations of ksh (ksh93, mksh), $PS1
is subject to command substitution and parameter expansion, so you can do things like:
function mywarning {
tput setaf 1; tput bold
date "+it's %T, please remember it's a production system"
tput sgr0
}
PS1='$(mywarning)# '
In ksh88 (which Solaris 10's /usr/bin/ksh
still is), only parameter and tilde expansion are done in the prompt.
You have to revert to clever tricks for anything fancy in the prompt. And then, you're quite limited in what you can do.
While arithmetic expansion is not performed, as you can expand things like ${array[expr]}
, you can have arithmetic expressions (here expr
) evaluated. Because assignments (though only to numerical values) can be done in arithmetic expressions, that gives some opportunities to do fancy things.
You can use that to post-process dynamic variables like $SECONDS
, $PWD
, $?
, $!
, $RANDOM
, $_
.
For instance, you can get a HH:MM:SS date with:
typeset -Z2 _h _m _s
eval "$(date +'SECONDS=$((%H*3600+%M*60+%S))')"
PS1='${z[(_h=SECONDS/3600)+(_m=(SECONDS/60)%60)+(_s=SECONDS%60)]}${_h}:${_m}:${_s} $ '
You can add your fat warning every 5 prompts by doing:
set -A warn '' '
[THIS IS A PRODUCTION SYSTEM!!]
' '' '' ''
PS1='${warn[warn_count=(warn_count+1)%5]}# '
I don't think you can have a command run before each prompt (the mail check is done internally by ksh
), but you can have one run after each command with the DEBUG trap:
trap 'my-command' DEBUG
Note that it's done after every simple command in every subshell.
So for instance, in:
for f in *; do :; done
my-command
will be run as many times as there are files in the current directory.
In:
: | :
It is run 3 times... So in effect, it's not really practical.
export PS1='$(date -u +%s) ${ME}:${PWD}# '