1

I used to know this but I forgot: I want to execute a command or a couple of commands before every single prompt. When I am in our PROD box, I want a big fat reminder above every prompt that says * YOU ARE IN PROD * and maybe another thing or two. Currently, I think that is how mail is checked but I can't remember how that configuration is set.

Using ksh and Solaris 5.10

Any idea?

Thanks

4
  • 2
    Bash has PROMPT_COMMAND, but not ksh. Couldn't you just set your warning in your PS1?
    – jordanm
    Oct 4, 2012 at 16:37
  • yes but that is just a static variable. let's say i want to run a command before every prompt, e.g. display the current size of my home directory (this was an arbitrary example but you get the point).
    – amphibient
    Oct 4, 2012 at 16:49
  • e.g. where is it specified that mail should be checked before every prompt?
    – amphibient
    Oct 4, 2012 at 16:50
  • 2
    I'm not a ksh user, so leaving this for someone else to answer. But this seems to work for me (updating each time the prompt is renewed): export PS1='$(date -u +%s) ${ME}:${PWD}# ' Oct 4, 2012 at 17:04

3 Answers 3

3

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.

2
  • 1
    This only works in specific versions of ksh: M-12/28/93e works - M-11/16/88f does not.
    – user14755
    Aug 15, 2014 at 3:35
  • @DarkHeart, Indeed, I've updated the answer. Aug 15, 2014 at 9:27
0

I'd simply put it into /etc/profile, or /etc/issue. Sometimes displaying the contents of the issue file is turned off, but I'd still think it's a good reminder.

Changing the contents of the $PS1 variable is ok, I guess, you might want to still print some warnings /etc/profile. Since it's a production system, It's ok to edit those files, since here, you want lasting changes in the system. (Normally, I'd advise against changing those files and using user files instead.)

0

You can see the host, time, locations and git branch with:

HOST='\033[02;36m\]\h'; HOST=' '$HOST
TIME='\033[01;31m\]\t \033[01;32m\]'
LOCATION=' \033[01;34m\]`pwd | sed "s#\(/[^/]\{1,\}/[^/]\{1,\}/[^/]\{1,\}/\).*\(/[^/]\{1,\}/[^/]\{1,\}\)/\{0,1\}#\1_\2#g"`'
BRANCH=' \033[00;33m\]$(git_branch)\[\033[00m\]\n\$ '
PS1=$TIME$USER$HOST$LOCATION$BRANCH

You could modify any of these, e.g. host.

If you want to run a command use backtics - these ``

1
  • That's bash syntax. The OP is looking for a ksh solution. Probably even ksh88 since he's on Solaris (unless he uses /usr/dt/bin/dtksh) Aug 15, 2014 at 17:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .