Storing a Korn shell program in a file
You won't always want to re-type your Korn shell instructions each time you want to run then, so you may store them in a file:
Here's the file "hello_korn"
# Demonstration of a file of Korn Shell commands
echo This example has been provided by Well House Consultants
echo -n "Copyright - " # Note quotes to add trailing space
date
ls !(h)*
and I can run it as follows:
$ ksh hello_korn
This example has been provided by Well House Consultants
Copyright - Sat Sep 22 10:38:05 BST 2007
hello_korn script_korn
$
But I have to KNOW that's a Korn script to run it - if I just want to type its name at the command line, I come across various issues:
$ hello_korn
ksh: hello_korn: not found
$ ./hello_korn
ksh: ./hello_korn: cannot execute - Permission denied
$
and even when I fix permissions:
$ ./hello_korn
This example has been provided by Well House Consultants
Copyright - Sat Sep 22 10:47:30 BST 2007
./hello_korn: line 8: syntax error near unexpected token `('
./hello_korn: line 8: `ls !(h)*'
$
Three things:
1. I should change permissions on the file to make it executable:
chmod a+x hello_korn
or similar
2. I should specifiy at the top of the file that it is to be run under the Korn shell rather than any other, by adding a line
#!/usr/bin/ksh
3. I might decide to change my path:
export PATH=$PATH:.
but bear in mind that there may be security implications of adding any executable files you have in the current directory onto the list of executable syou may run.
It then works:
$ script_korn
This example has been provided by Well House Consultants
Copyright - Sat Sep 22 10:50:40 BST 2007
hello_korn script_korn
$
THE LIFE OF KORN SHELL
A new Korn shell doesn't just start up in isolation; rather, it starts up by reading in some initial settings from
/etc/profile
and $HOME/.profile
if it's a login shell, and then from a file named in $ENV. And if it's a shell which has been called in by another shell, it will inherit environment variables and functions which have been exported by that other shell.
Note the /etc/profile will usually contain directives to run additional shell startup scripts such as
/etc/ksh.kshrc
and $HOME/.kshrc
If a shell variable called HISTFILE is set, then the history will be reloaded from that file; if HISTFILE is not set, there's no re-loading of a previous session.
Commands available to the Korn Shell user / programmer
Like in Tcl, "everything is a command" in Ksh programming. In other words, as you analyse the syntax of the statements of the language you'll always find that, underlying everything else, you have a command name followed by a series of arguments. But there are several groups of commands:
SHELL BUILT IN COMMANDS
Commands in this group are included within the shell program itself, and may vary somewhat from other commands with the same name in othe Unix or Linux shell programs (you have been warned now, right?)
.
:
[
alias
bg
break
builtin
cd
command
continue
echo
eval
exec
exit
export
false
fc
fg
getopts
jobs
kill
let
print
pwd
read
readonly
return
set
shift
test
times
trap
true
typeset
ulimit
umask
unalias
unset
wait
whence
COMPOUND STATEMENTS / COMMANDS
Rather like the built in commands, these may vary in their Korn shell implementation to their implementation in other shells - although any that you use in the Bourne shell should wok directly here in ksh.
!
[[
case
do
done
elif
else
esac
fi
for
function
if
in
select
then
time
until
while
{
}
ALIASED COMMANDS
You can make up your own commands too, using the alias command
$ alias 'll=ls -l'
$ ll
total 168
-rw-r--r-- 1 trainee users 38 2007-09-22 07:21 body
-rw-r--r-- 1 trainee users 39 2007-09-22 07:18 demo
-rw-r--r-- 1 trainee users 2643 2007-09-22 07:11 first
-rw-r--r-- 1 trainee users 101 2007-09-22 07:21 head
Although useful, you should take care not to rely on too many of your own aliases if you regularly move from one Korn shell environment to another - you need to know the basics.
The following aliases are built in to the Korn shell
autoload='typeset -fu'
functions='typeset -f'
hash='alias -t'
history='fc -l'
integer='typeset -i'
local=typeset
login='exec login'
newgrp='exec newgrp'
nohup='nohup '
r='fc -e -'
stop='kill -STOP'
suspend='kill -STOP $$'
type='whence -v'
unalias will let you delete an alias.
SEPARATE PROGRAMS
The majority - the vaste majority - of commands you need will be separate programs, and you'll find them on your computer / file system in the various directories listed in $PATH.
$ echo $PATH
/usr/local/jboss/bin:/usr/local/ant/bin:/opt/SUNWappserver/bin:
/opt/SUNWappserver/jdk/bin:/home/trainee/bin:/usr/local/bin:/usr/bin:
/usr/X11R6/bin:/bin:/usr/games:/opt/gnome/bin:/opt/kde3/bin:
/usr/lib/mit/bin:/usr/lib/mit/sbin:.
$
Even commands as fundamental as ls and cp will be found as separate programs.
If you need to find out what comes from where, have a look at the whence command
$ whence ls
/bin/ls
$ whence ant
/usr/bin/ant
$ whence pwd
pwd
$ whence greet
greet
$ whence ll
'ls -l'
$
or if you prefer
$ whence -v pwd
pwd is a shell builtin
$ whence -v ll
ll is an alias for 'ls -l'
$ whence -v greet
greet is a function
$ whence -v whence
whence is a shell builtin
$ whence -v ln
ln is /bin/ln
$
FUNCTIONS
You can define your own command (script) within your script - nested scripts within the same shell if you like - as a function. A sort of heavy-weight alias, if you like.
$ vi funky
$ . ./funky
Hello World
$ greet
Hello World
$
Let's see the funky file:
#!/usr/bin/ksh
function greet {
echo "Hello World"
}
greet
Some common environment variables and settings
The set command allows lots of shell settings (switches) to be thrown.
-C noclobber
-n noexec (for checking)
-v verbose
-x xtrace - very useful for learning about how scripts work!
vi allows vi style command line editing
Environment:
CDPATH PATH EDITOR HISTFILE OLDPWD PPID PS1, 2 3 and 4 SECONDS
See also
Linux Basics Course