Open Source Software and Linux:

korn shell

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 25 2009   1:22AM GMT

Using the Korn Shell with Linux



Posted by: John Little
korn shell, bash, Linux, unix, scripting

My current consulting gig requires that I use the Korn Shell and modify Unix scripts so that they will work with Linux. While the Korn Shell has many comparable characteristics of BASH there are some distinct differences-or at least ones that I’ve never seen in BASH.

The first difference that I noticed is tab completion. For example let’s say that I issue the command

ls /home/jlittle

and hit the tab key to see the files and directories. The output that you see will be in this format

ls /home/jlittle/
1) CentOS-5.2-x86_64-bin-DVD/
2) Desktop/
3) Documents/
4) Video call snapshot 8.png
5) bin/
6) ffmpeg.cfg

At this point you can either choose a number and hit the tab key or type in the first couple of letter of what you want to see or do. The complete output when using the number would look like this

ls /home/jlittle/<tab>
1) CentOS-5.2-x86_64-bin-DVD/
2) Desktop/
3) Documents/
4) Video call snapshot 8.png
5) bin/
6) ffmpeg.cfg
ls /home/jlittle/Desktop/<2tab>
Project-timeSheet.ods Skype.desktop

Typing 2 tab and the tab completion gives us the listing of the /home/jlittle/. Kind of a cool way of doing tab completion don’t you think?

You should also not use the “test” built-in that is available in bash. In bash the test built-in is the same as the “[" built-in. In other words don't use

if test $# -gt 0; then

instead use:

if [ $# -gt 0 ]; then

The korn shell also prefers the use of double brackets syntax “[[ ]]” instead of single brackets. This adds additional operators such as && and ||:

if [[ $# -gt 0 && $? -eq 0 ]]; then

You can use && and || to construct shorthand for an “if” statement in the case where the if statement has a single consequent line:

[ $# -eq 0 ] && exit 0

The Korn Shell is a powerful tool that can make your job easier. Since it’s creation several features have been added while maintaining backwards compatibility with the Bourne shell. The Korn shell can also be used as a programming language which gives it a distinct advantage of typical Unix and Linux shells.

Give ksh a whirl. I haven’t even scratched the surface of what the Korn shell can do for your scripting. If you are used to scripting with Bash then learning the Korn shell should only have a mild learning curve while presenting you with additional scripting power and speed.

-j


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