Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
Korn Shell - History, storing to file, commands

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

Please note that articles in this section of our web site were current and correct to the best of our ability when published, but by the nature of our business may go out of date quite quickly. The quoting of a price, contract term or any other information in this area of our website is NOT an offer to supply now on those terms - please check back via our main web site

Related Material

Web Application Deployment - Korn Shell - interactive and programming facilities
  [1361] - ()
  [1364] - ()
  [1365] - ()
  [1367] - ()
  [1658] - ()
  [1659] - ()
  [1660] - ()

Web Application Deployment - Linux Utilities
  [63] - ()
  [71] - ()
  [1361] - ()
  [1366] - ()
  [1690] - ()
  [2145] - ()
  [2320] - ()
  [2484] - ()
  [2638] - ()
  [3446] - ()
  [3764] - ()
  [3902] - ()
  [4586] - ()
  [4682] - ()

Web Application Deployment - Linux -An Introduction For Users
  [73] - ()
  [74] - ()
  [152] - ()
  [249] - ()
  [430] - ()
  [431] - ()
  [593] - ()
  [659] - ()
  [679] - ()
  [703] - ()
  [710] - ()
  [711] - ()
  [749] - ()
  [984] - ()
  [1012] - ()
  [1013] - ()
  [1068] - ()
  [1259] - ()
  [1287] - ()
  [1288] - ()
  [1366] - ()
  [1408] - ()
  [1438] - ()
  [1527] - ()
  [1651] - ()
  [1764] - ()
  [1803] - ()
  [1893] - ()
  [1897] - ()
  [1902] - ()
  [1904] - ()
  [2201] - ()
  [2203] - ()
  [2299] - ()
  [2300] - ()
  [2479] - ()
  [2494] - ()
  [2636] - ()
  [2831] - ()
  [3179] - ()
  [3256] - ()
  [3791] - ()
  [3819] - ()

resource index - Deployment
Solutions centre home page

You'll find shorter technical items at The Horse's Mouth and delegate's questions answered at the Opentalk forum.

At Well House Consultants, we provide training courses on subjects such as Ruby, Lua, Perl, Python, Linux, C, C++, Tcl/Tk, Tomcat, PHP and MySQL. We're asked (and answer) many questions, and answers to those which are of general interest are published in this area of our site.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2022: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/solutions/general- ... mands.html • PAGE BUILT: Wed Mar 28 07:47:11 2012 • BUILD SYSTEM: wizard