Ksh archives - Open Source Software and Linux

Open Source Software and Linux:

ksh

Feb 6 2009   2:49PM GMT

Solaris 10, ksh and root



Posted by: John Little
solaris 10, ksh, korn shell, bourne shell, /root, default shell, /sbin/sh

Like many people I have the question of, on Solaris 10, can I set the default root shell to ksh. After some study and research apparently it is ok to do this. The default shell for root is /sbin/sh. For those of us that use Linux this is not a symlink to bash but a static binary of the Bourne Shell.

Solaris versions previous to 10 had the Bourne Shell statically linked so that in case of a crash root would have access to a shell. This implies that certain directories were not mounted when booting into a ‘rescue’ mode, namely /usr.

With version 10 of Solaris, Sun has dynamically linked linked the Bourne Shell. This means that it does in fact now use shared libraries. This also implies that since other shells use shared libraries that it is ok to use the shell of your choice.

Sun has also built code into the OS that if for some reason the shell that is designated as the default is not accessible it will fall back to /sbin/sh. This resolves the problem of changing the default shell and still being able to sleep at night. Still experience says this is new and different, do I really want to rely on this new style?

If you are like me, a devout coward regarding such things, then you probably are reluctant to go this route. Because of my cowardice I have reached a compromise that works well, at least for me.

First I created a /root home directory and changed the home directory in /etc/passwd to reflect this. The directory mode is set to 700 and user/group ownership is set to root.

I then copied the contents of /etc/skel, including the hidden file .profile, into root’s home directory. After that I edited the .profile file so that it contains the following:

SHELL=/bin/ksh
export SHELL
HISTFILE=~/.history
HISTSIZE=1000

This gives me a login and working shell of ksh while leaving the default shell in /etc/passwd to /sbin/sh.

hth.

John

Jan 24 2009   8:01PM GMT

Setting script variables using the if..then statement



Posted by: John Little
Linux, unix, ksh, korn shell, bash, if..then, variable

Recently I found myself wanting to set some variables in a script that I was writing using the if..then statement. I am using the Korn shell but this is bash compatible.

I wanted to do this as the variable command changed depending on if the script was running on Linux, HP-UX or AIX. This saved a ton of typing the same test over and over throughout the script which is about 600 lines.

The basic form of the script is

if [[ $STRING != string ]]; then
VARIABLE=string1
else
VARIABLE=string2
fi

As you can see the if statement is built around the VARIABLE rather than VARIABLE=if statement. Doing it in that form either won’t return any output or give you output that is not useful.

A real world example

VENDOR=$(uname)
if [ $VENDOR != "Linux" ];then
BACKUPHEADER=log “$(date ‘+%D %T’) Checking header.”
else
BACKUPHEADER=/bin/logger -t “VALIDATE LINUX BACKUP: $(date ‘+%D %T’) Checking header.”
fi
[code]
As you can see from the above the Unix command for sending output to syslog is log and is logger on Linux. Now imagine having to run various tests throughout the script and sending the output to syslog depending on the OS. As I mentioned, it saved quite a bit of typing.

Hope this helps you somewhere!

-j