UNIX projects
danishenglishPDF-icon
Security6.net logo

UNIX projects

I like UNIX and I really dont want to use anything else.

When I say UNIX I think of any of the UNIX-like operating systems such as OpenBSD, Mac OS X, AIX, Linux, FreeBSD, ...

I also teach courses in UNIX and I have some knowledge I would like to share.

The login profile


I tell people attending my UNIX courses that they should take some time to develop a login profile that can be used in many places and even if it takes a week it is time well spent. Having the right prompt, having the right aliases means that you as a UNIX user will be more productive - and make fewer errors.
Think of this scenario; you have 5-6 terminal windows open on your display of which some are connected to other servers. You want to shut down one of these machines and you go on to type the shutdown command:
$ shutdown now
Then you hear the phone ring almost immediately - ohhh ohhh - it was the wrong machine! I have tried that and now I try to make sure that I always use a prompt that contain the hostname of the server I am connected to:
hlk@sunny:hlk$
This helps me when I have a lot of terminal windows lying around on the desktop.

The PS1 prompt

If you want to have the prompt above with user@nameserver:directory I can recommend these settings for Korn shell
UID=`id -u`
case $UID in
0) PS1S='# ';;
esac
PS1S=${PS1S:-'$ '}
HOSTNAME=${HOSTNAME:-`uname -n`}
HOST=${HOSTNAME%%.*}
# Set prompt
PS1='$USER@$HOST:${PWD##*/}$PS1S'
This will make your root prompt be a # and other users have $, while having the hostname without the domain name and only the last part of the working directory is shown.

Which shell to use

It depends on the systems you use ... and I have chosen Korn shell since it is available on the systems I use, which are mostly OpenBSD, Mac OS X and AIX. Korn shell come in two variants which are the real korn shell written by David Korn ksh and pdksh which is a public domain implementation of the Korn shell language.
I will happily use both and they are both excellent.

Where is the shell - and which version is it?

While kdh and pdksh are both available in source format and thus can be compiled on most UNIX-like systems there are a lot of differences when trying to find a binary shell called ksh on the systems I use.
  • OpenBSD which is my primary server OS has pdksh included in the base operating system and it is available as /bin/ksh, which is also the same as /bin/sh
  • AIX has both ksh93 and ksh88 which is the old korn shell. The are installed by default and are available in /bin/ksh (ksh88) and /bin/ksh93
  • Mac OS X has ksh in the base operating system and pdksh can easily be installed from darwinports
  • FreeBSD has pdksh in ports-tree
So to summarize which Korn shell is available, where it originates from and the systems I am interested in:
Operating system
ksh available?
PATH Col 31 Col 41 Col 51
OpenBSD pdksh in base OS
/bin/ksh      
Mac OS X
 ksh93 in base OS
/bin/ksh      
Mac OS X
pdksh in darwinports
/usr/local/bin/ksh
     
 AIX ksh88 and ksh93 in base OS /bin/ksh and /bin/ksh93      
 FreeBSD  pskdh in ports  /usr/local/bin/ksh      

You can check your version of ksh by pressing ctrl-v when using ksh93 and by showing the KSH_VERSION environment variable in pdksh
$ uname -a
Darwin harry 8.6.0 Darwin Kernel Version 8.6.0:...
$ /bin/ksh
$ (press ctrl-v) will show: Version M 1993-12-28 p
$ /usr/local/bin/ksh
$ echo $KSH_VERSION
@(#)PD KSH v5.2.14 99/07/13.2

Developing the perfect login profile

How do you go about making the perfect login profile?
Inspired by a book on developing CSS I decided to split this into parts, which can then be combined later on.

  • selecting a shell, since ksh is not available in the base OS and I dont want to be locked out I will implement a /bin/sh profile which can then exec into a Korn shell environment
  • main variables which needs to have sensible values, PATH, PS1 etc.
Note: most of these tricks are NOT mine - I have used the internet and a lot comes from the profile example by Morten Liebach, which is inspired from other profiles, which are probably inspired by others.

Login profile requirements

I want to build a profile which can be executed unmodified on all systems: OpenBSD, Mac OS X, AIX and FreeBSD. If the shell is configured with a ksh it should just work, but if the shell is a Bourne shell it should check if a ksh is available and switch to that.

Executing the right shell

If my shell is not a korn shell I will execute a korn shell. Since my preference is for pdksh I will look for ksh in /usr/local/bin first and then /bin/ksh.
Since pdksh has the variable KSH_VERSION I will use that to check which version I am running - in case I make some profile stuff that is incompatible between ksh and pdksh.

References



Korn shell home page is: http://www.kornshell.com/ Note: while ksh93 source is available it is not public domain
Public Domain Korn shell home page is: http://www.math.mun.ca/~michael/pdksh/ Morten Liebach has a number of dotfiles on the internet in his pub/dotfiles directory including a profile for Korn shell which has been the base for my current Korn shell setup.