Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
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
  [1660] Korn shell - some nuggets - (2008-05-30)
  [1659] String, Integer, Array, Associative Array - ksh variables - (2008-05-30)
  [1658] Some useful variables and settings in the Korn Shell - (2008-05-30)
  [1367] korn tips - some useful korn shell techniques - (2007-09-25)
  [1365] Korn Shell scripts on the web - (2007-09-25)
  [1364] Korn shell course - resources - (2007-09-24)
  [1361] Korn shell course - (2007-09-22)

Web Application Deployment - Linux Utilities
  [3902] Shell - Grep - Sed - Awk - Perl - Python - which to use when? - (2012-10-22)
  [3764] Shell, Awk, Perl of Python? - (2012-06-14)
  [3446] Awk v Perl - (2011-09-18)
  [2638] Finding what has changed - Linux / Unix - (2010-02-17)
  [2484] Finding text and what surrounds it - contextual grep - (2009-10-30)
  [2320] Helping new arrivals find out about source code examples - (2009-08-03)
  [2145] Using the internet to remotely check for power failure at home (PHP) - (2009-04-29)
  [1690] Conversion of c/r line ends to l/f line ends - (2008-06-28)
  [1366] awk - a powerful data extraction and manipulation tool - (2007-09-25)
  [1361] Korn shell course - (2007-09-22)
  [71] Comparators in Linux and Unix - (2004-10-03)
  [63] Almost like old times - (2004-09-26)

Web Application Deployment - Linux -An Introduction For Users
  [3819] Packing a tar, jar or war file - best practise - (2012-07-26)
  [3791] The Kernel, Shells and Daemons. Greek Gods in computing - (2012-07-01)
  [3256] Displaying a directory or file system tree - Linux - (2011-04-22)
  [3179] Oops - I typed ci not vi, and have lost my file ... - (2011-02-21)
  [2831] Recording (a macro) in vi - (2010-06-27)
  [2636] Linux - useful tips including history and file name completion - (2010-02-15)
  [2494] Making Linux Politically correct - (2009-11-06)
  [2479] Accidentally typed ci rather than vi? - (2009-10-27)
  [2300] What does x on a linux directory mean? - (2009-07-21)
  [2299] How much space does my directory take - Linux - (2009-07-20)
  [2203] Always use su with minus. And where do programs come from? - (2009-05-27)
  [2201] Running straight from the jar, but not from a tar - (2009-05-26)
  [1904] Ruby, Perl, Linux, MySQL - some training notes - (2008-11-23)
  [1902] sstrwxrwxrwx - Unix and Linux file permissions - (2008-11-23)
  [1897] Keeping on an even keel - (2008-11-21)
  [1893] Some Linux and Unix tips - (2008-11-18)
  [1803] FTP passive mode - a sometimes cure for upload hangs - (2008-09-20)
  [1764] Yank and Push - copy and move in vi - (2008-08-21)
  [1651] ls command - favourite options - (2008-05-23)
  [1527] Selecting file names in a shell - one word or another - (2008-02-02)
  [1438] Copy and paste / cut and paste and other vi techniques - (2007-11-20)
  [1408] Wireless hotel tips - FTP and Skype connections failing - (2007-10-26)
  [1366] awk - a powerful data extraction and manipulation tool - (2007-09-25)
  [1288] Linux run states, shell special commands, and directory structures - (2007-08-03)
  [1287] Work and play at Well House Manor - Football and Shell Shortcuts - (2007-08-02)
  [1259] Where am I and how did I get here? - (2007-07-05)
  [1068] ls -l report, Linux / Unix - types and permssions - (2007-02-06)
  [1013] Copy multiple files - confusing error message from cp - (2006-12-30)
  [1012] Moving files between Windows / DOS and Linux / Unix - (2006-12-30)
  [984] Cardinal numbers and magic numbers - (2006-12-14)
  [749] Cottage industry or production line data handling methods - (2006-06-07)
  [711] THE home directory or MY home directory - (2006-05-06)
  [710] Linux training Glasgow, Python programming course Dundee - (2006-05-05)
  [703] Copying files and preserving ownership - (2006-04-28)
  [679] More or less on the edge of the page - (2006-04-11)
  [659] Web Application Components - (2006-03-28)
  [593] Finding where the disc space has gone - (2006-02-06)
  [431] File permissions of Linux and Unix systems - (2005-08-31)
  [430] Linux commands - some basics - (2005-08-31)
  [249] An easy way out - (2005-03-17)
  [152] Aladdin, or careful what you wish. - (2004-12-15)
  [74] pushd and popd - (2004-10-05)
  [73] vi - full circle - (2004-10-04)

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., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • 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