Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
Python and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Lua, etc
 
Korn Shell - Basics

What is a Shell Program / Script?

A Shell is a program which reads user inputs and controls program output - a command line interpretter. Instructions typed in are split down into component parts (tokenized) and acted on accordingly - typically with the computer's underlting operating system running one or more programs or commands, and returning the output of those programs as described within the instruction.

The term "Shell" is typically used in the Unix and Linux world - a DOS command line interpretter is the near equivalent for Windows.

Where you have a requirement to rerun the same series of commands on a Unix / Linux system, you can save your commands / instructions into a file and then perform the file as a whole - and that is commonly known as a "Shell Script" or "Shell Program".

All mainstream programming languages include conditional constructs (to allow the programmer to specify code which may or may not be performed depending on the result of a test), loops (to allow a specific piece of code to be repeatedly executed a number of times without it having to be repeated in the program), variables (to allow information to be stored within the program from one operation through to following ones) and functions (to allow a piece of code that's required multiple times to be called up by a single easy name whenever it's needed). Shells are no exception - they include conditionals, loops, variables, and functions.

Why are there several different shells available?

The original shell - knows as the Bourne Shell - is found in a file called "sh". Dating back many years now, to the days of printing terminals and batch computing, it was (and is) very strong on programming facilities compared to facilities that are really needed for good command line interaction. The Bourne shell remains the bedrock of Unix and Linux systems, with the operating system's internal scripts that are run at startup and shutdown time programmed in "sh".

In order to provide better interaction, The C Shell was developed and this provided far greater interaction facilities - for example, the ability to rerun a previous command which is something that is exceedingly useful on the command line, but pretty much unwanted in batch files. The C Shell was adopted as the standard for many users, especially on Unix varients such as SunOS / Solaris.

In order to make the best of both worlds, the Korn Shell (ksh) was developed. Compatible with the Bourne Shell, but adding to it the intercative capabiities (all be it with a different syntax) of the C shell, it was / is intended to provide the best of both worlds.

And there are other shells you'll come across too - ranging from "bash" - the bourne again shell - which is very popular on Linux systems, through tcsh (an open source varient of the C shell) right up the alphabet to zsh. You may also see reference to the pdksh - the public domain Korn shell - which is in practise the flavour used in Linux.

Some exceptions to the "just another shell" rule - note that rsh and ssh are remote shell commands / protocols - not really shells in their own rights, but mechanisms through which shells can be accessed via a network. Rsh allows you to run a command on a remote server, and the ssh program allows you to conatct one machine from a nother on a network, and run commands remotely with the traffic between the two hosts being encrypted to stop intermediate snooping.

Korn Shell Basics

-* Each instruction starts with the name of the command (which may be something that's built in to the shell, or a separate operating system command).

-* Instructions are followed, optionally, by a number of parameters which are separated from one another by white space

-* Instructions are terminated by a new line character.

Example:

$ grep -c public Temperatures.java WellHouseInput.java
Temperatures.java:2
WellHouseInput.java:6
$

The instruction is a seperate command called "grep" in this case, and it has four parameters. The first is "-c, the second is "public", the third is "Temperatures.java" and the fourth is "WellHouseInput.java". When run on the machine these notes were written on, you'll see that we typed the command against as shell prompt "$" character, and it produced two lines of output.

Metacharacters

As well as tokenising and passing through commands, shells support a number of metacharacters which effect how they do that parsing and what they do as they run commands.

PIPING AND REDIRECTION

> redirects the output from a command (on what is known as STDOUT or the regular job output) to a file named thereafter. If the file already exists, the prior contents are cleared out first.

>> redirects the output from a command (on what is known as STDOUT or the regular job output) to a file named thereafter. If the file already exists, new contents are added on to the end

< redirects the input to a command (know as STDIN or the regular input) from the named file rather than from the keyboard.

| diverts the STDOUT of one command in to the STDIN channel of the next one, thus making a production line of commands.

$ ls .. > first
$ date > second
$ ls .. >> second
$ cat -n second | more
     1 Sat Sep 22 07:11:39 BST 2007
     2 ABC
     3 abc.html
     4 ac_20060330.xyz
     5 ac_20060331.xyz
     6 ac_20070123.xyz
(etc)
$ cat -n < first
     1 ABC
     2 abc.html
     3 ac_20060330.xyz
     4 ac_20060331.xyz
     5 ac_20070123.xyz

As well as STDOUT, commands have a second output channel known as STDERR which is used for error messages. The idea of STDERR being separate is that you can direct the regular output to a file for later use, but still see the error messages on the screen. In the Korn shell, you can divert STDERR too using 2>.

$ ls sfdsdf
ls: sfdsdf: No such file or directory
$ ls sdfsdf > demo
ls: sdfsdf: No such file or directory
$ ls hjsdfhj 2> demo
$ cat demo
ls: hjsdfhj: No such file or directory
$ ls / wobble > head 2> body
$ cat head
/:
bin
boot
dev

etc

$ cat body
ls: wobble: No such file or directory
$

You can also combine STDOUT and STDERR to the same file using 2>&1. Doing two separate diverts to the same file will NOT work, as one output overrides the other.

$ ls / wobble > hh 2>&1
$ ls / wobble > ii 2> ii
$ diff ii hh
0a1
> ls: wobble: No such file or directory
$

Other combined uses ... you'll often pipe, pipe, pipe, pipe, redirect.

Example:

$ gzip -dc /usr/share/man/man1/ksh.1.gz | groff -T html > ksh.html
$ ls -l
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
-rw-r--r-- 1 trainee users 139 2007-09-22 07:22 hh
-rw-r--r-- 1 trainee users 101 2007-09-22 07:24 ii
-rw-r--r-- 1 trainee users 136357 2007-09-22 07:36 ksh.html
-rw-r--r-- 1 trainee users 2672 2007-09-22 07:11 second

WILD CARDS

If you write *.txt to match all the file names that end in .txt, it's actually the shell that's doing the work and providing the extra parameters. You can see this in the Korn shell by doing a set -x, then using a wildcard:

$ set -x
$ ls -l *.*
+ ls -l ksh.html ksh.txt
-rw-r--r-- 1 trainee users 1150 2007-09-22 07:47 ksh.html
-rw-r--r-- 1 trainee users 127555 2007-09-22 07:45 ksh.txt
$

This means that you can use the following wildcards to match EXISTING FILE NAMES ONLY (different to Windows Wild cards)

 * - match 0 or more characters
 ? - match any one character
 [xyz] - match one character from the list
 ~ - shorthand for your home directory
 ~fred - shorthand for fred's home directory.

Note that a range [a-z] is allowed, as is a ! for not - [!aeiou]

Other Korn shell "Wild cards" are:

 @(...|...) - match one occurrence of any option
 +(...|...) - match one or more occurrences of any option
 *(...|...) - match zero or more occurrences of any option
 ?(...|...) - match zero or one occurrence of any option
 !(...|...) - match anything that does NOT include any option

Examples:

+ ls
body demo first head hh ii ksh.html ksh.txt second
$ ls +(ks|he)*
+ ls head ksh.html ksh.txt
head ksh.html ksh.txt
$ ls !(hh|ii)
+ ls body demo first head ksh.html ksh.txt second
body demo first head ksh.html ksh.txt second
$

Although not REALLY wildcards, don't forget . and .. for "current directory" and "parent directory" respectively.

As with all Unix and Linux shells, you must NOT try to use these wild cards to create new file names.

QUOTING

If you enclose part of your command line in ' characters, you're saying that you wantit taken as a single parameter. Using " characters instead is similar, but allows any shell variable mentioned within to be expanded. And a \ character removes the special significance from just the following character.

$ cat peter john glover
cat: peter: No such file or directory
cat: john: No such file or directory
cat: glover: No such file or directory
$ cat "peter john glover"
cat: peter john glover: No such file or directory
$ cat 'peter john glover'
cat: peter john glover: No such file or directory
$ cat peter\ john glover
cat: peter john: No such file or directory
cat: glover: No such file or directory
$

If you use BACKQUOTES, you're doing the opposite of protecting a part of your command line - you're isntructing the shell to perform the contents before it performs the rest of the command, and substitute the information in place. Thus

$ date +%A
Saturday
$ ls > `date +%A`.txt
$ ls -l Sat*
-rw-r--r-- 1 trainee users 64 2007-09-22 09:26 Saturday.txt
$

CONTINUATION AND SPLITTING A LINE

You may specify several commands on one line, separating them with a ; characted, and you may use a \ to continue on form one line to another.

$ ls \
> -l
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
-rw-r--r-- 1 trainee users 139 2007-09-22 07:22 hh
-rw-r--r-- 1 trainee users 101 2007-09-22 07:24 ii
-rw-r--r-- 1 trainee users 1150 2007-09-22 07:47 ksh.html
-rw-r--r-- 1 trainee users 127555 2007-09-22 07:45 ksh.txt
-rw-r--r-- 1 trainee users 64 2007-09-22 09:26 Saturday.txt
-rw-r--r-- 1 trainee users 2672 2007-09-22 07:11 second
$ pwd ; ls
/home/trainee/korn
body demo first head hh ii ksh.html ksh.txt Saturday.txt second
$

ENVIRONMENT VARIABLES

When you start a new shell, you're provided with a number of "shell variables" - named memory locations with values relating to the current shell set in them.

You may use them within your commands by preceeding them with a $.

You may set them using an "=" assignment - NO DOLLAR is this case, please! (You never use a dollar when setting a variable in Shell - only when using it in a readonly context)

The default environment variables that you're provided with effect the operation of your shell or commands called up within it in some way or other, and if you change the values they contain you'll effect the subsequent behaviour of the shell and its children.

$ echo $WINDOWMANAGER
/usr/X11R6/bin/kde
$
$ PS1='Hello: '
Hello: pwd
/home/trainee/korn
Hello: PS1='$ '
$

COMMENTS

In the Korn shell, any TOKEN that starts with a # character indicates that the rest of that command is to be treated as a comment.

$ echo "The # chacater starts a comment"
The # chacater starts a comment
$ echo If I type a # in a line
If I type a
$ # This is a comment
$

You are strongly encouraged to comment your scripts, using

- Blank lines to separate logical code blocks
- Major comments on line starting with # characters
- Specific notes about individual commands on the line ends

THE HISTORY MECHANISM

The Korn shell remembers the commands that you have run, and makes them available to you through the history mechanism. The actual command used is called fc, but it's aliased in several ways to other names.

$ history
29 PS1='Hello: '
30 pwd
31 PS1='$ '
32 echo The # character starts a comment
33 echo The # character oh - \
34 echo "The # chacater starts a comment"
35 echo If I type a # in a line
36 # This is a comment
37 alias
38 alias 'll=ls -l'
39 ll
40 ll /
41 FCEDIT=vi
42 export FCEDIT=vi
43
44 alias
$ r 35
echo If I type a # in a line
If I type a
$

Also note that $_ is an environent variable that means "last parameter, previous line".

You can also set up a command line editor so that you can interactively use your history list:

$ set -o vi
$

Then - ESC to enter command mode, KJHL for navigation, i, x, a and more.


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
  [4682] One line scripts - Awk, Perl and Ruby - (2016-05-20)
  [4586] Extending your bash shell with aliases, functions and extra commands - (2015-11-28)
  [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., 2019: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01225 708225 • FAX: 01225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

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