Learn Programming

KornShell scripting

Introduction

KornShell scripts let you type a whole lot of commands in a script file and then you only have to enter the script file name to run all of the commands in the script file at once.

You need to use the vi editor to type your scripts. Open a new file in vi like this:

$vi myscript.ksh

You must remember to save the file and exit vi when you want to run the script. Before you can run the script you will have to give yourself execute rights on the file:

$chmod u+x myscript.ksh

You can run the script by typing the name of it:

$myscript.ksh

Usage line

The first line of every script must be the usage line. You will learn a bit more about the usage line later. Here is what the usage like should look like:

USAGE="Usage: myscript.ksh"

Comments

If you want to make a comment on a line of the script then type it after a # (hash). Anyhting after a # is not executed when the script runs. Comments are used to help you understand what a script does.

USAGE="Usage: myscript.ksh" #The usage line

Using console commands in scripts

You can use any console command in a script. Here is an example and the output:

USAGE="Usage: myscript.ksh"
whoami
pwd
ls
 
$myscript.ksh
johnny
/home/johnny
documents/
myscript.ksh
hello.c

Printing

You can print things on the screen with either the echo or the print command. Anything you want to print must be between inverted commas.

USAGE="Usage: myscript.ksh"
echo "Hello"
print " World"
 
Hello World

You can use \n to go to the next line when printing.

USAGE="Usage: myscript.ksh"
print "Hello \n World"
 
Hello
World

Variables

Variables are places in memory where data is stored. Names are given to variables so that we can use them without having to type in the memory location addresses. There are 2 types of variables which are strings and integers. Strings are for words and integers are for numbers. Strings must always be put between inverted commas.

Variable names should be typed in capital letters and must not start with a number. Variables come into existance when you first use their name. To assign a value to a variable you must use = (equals).

USAGE="Usage: myscript.ksh"
ANIMAL="cat"

When you print the value of a variable you must use $ (dollar sign) to show that it is a variable.

USAGE="Usage: myscript.ksh"
ANIMAL="cat"
print "$ANIMAL"

Before you can use an integer variable you must first declare it as an integer so that the computer knows it is not a string.

USAGE="Usage: myscript.ksh"
integer NUMBER
NUMBER=123

You can perform calulations on integers because they are numbers. When you perform a calculation you have to assign the answer to a variable. This variable can be one of the variables used in the calculation.

USAGE="Usage: myscript.ksh"
integer NUMBER1
integer NUMBER2
NUMBER1=NUMBER1+NUMBER2

The read command lets the user enter values. The entered values have to be stored in variables so that the computer remembers them.

USAGE="Usage: myscript.ksh"
print "What animal do you like?"
read ANIMAL
print "You like a $ANIMAL"

if then else

You can use if then else to make decisions in a script.

USAGE="Usage: myscript.ksh"
print "What animal do you like?"
read ANIMAL
if ((ANIMAL=="cat")) then
   print "Good"
else
   print "Bad"
fi

When you test a condition you must use == and not just one = because = is for assigning values and == is for testing values. Here is a table of the kind of tests that can be performed with if:

==Equal
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to
!=Not equal to

Menus and Case

A menu can be used in a script in the following way:

USAGE="Usage: myscript.ksh"
PS3="Choose a language:"
select $MENU_CHOICE in English German French
do
   case $MENU_CHOICE in
      English) print "You chose English"
      German) print "Sie wählten Deutsches"
      French) print "Vous avez choisi le français"
   Esac
done

You can also have an exit option. Remember that if you want to use more than one word as a choice then you have to put it between inverted commas. You will probably also want to tell the user if they have entered an invalid option. To do this you must use a *.

USAGE="Usage: myscript.ksh"
PS3="Choose a language:"
select $MENU_CHOICE in English German French "stop script"
do
   case $MENU_CHOICE in
      English) print "You chose English";;
      German) print "Sie wählten Deutsches";;
      French) print "Vous avez choisi le français";;
      "stop script") exit;;
      *) print "Invalid choice";;
   Esac
done

Loops

There are 3 kinds of loops which are the while loop, until loop and the for loop.

The while loop repeats while a condition is true.

USAGE="Usage: myscript.ksh"
integer i=1
while ((i<5))
do
   print "$i"
   i=i+1
done

The until loop repeats until a condition is true.

USAGE="Usage: myscript.ksh"
integer i=1
until ((i==5))
do
   print "$i"
   i=i+1
done

The for loop goes through a list of numbers before it leaves the loop.

USAGE="Usage: myscript.ksh"
for i in 1 2 3 4 5
do
   print "$i"
done

break lets you exit a loop at any time. continue lets you jump to the next iteration of the loop. The following example uses an always true loop to show how it is done:

USAGE="Usage: myscript.ksh"
integer i=1
while :
do
   print "$i"
   i=i+1
   if i<5 then
      continue
   fi
   break
done

Arrays

An array is a type of variable that can hold a lot of values using only one variable name. Each element or value in the array is given a number to identify it and this number must be put between square brackets.

USAGE="Usage: myscript.ksh"
ANIMAL[1]="cat"
ANIMAL[2]="dog"
ANIMAL[3]="rabbit"

You can print the value in the same way as you would with a normal variable except that you must use curly brackets.

USAGE="Usage: myscript.ksh"
ANIMAL[1]="cat"
ANIMAL[2]="dog"
ANIMAL[3]="rabbit"
print "${ANIMAL[1]}"
print "${ANIMAL[2]}"
print "${ANIMAL[3]}"

Arrays are much more useful if you use them with loops.

USAGE="Usage: myscript.ksh"
ANIMAL[1]="cat"
ANIMAL[2]="dog"
ANIMAL[3]="rabbit"
integer i
for i in 1 2 3
do
   print "${ANIMAL[$i]}"
done

Command line parameters

You already know that commands such as ls can take parameters for example ls -l. A script can also take parameters. Here is an example:

$myscript cat dog

The first parameter is always the script name that was entered and is accessed with $0. The other parameters would be $1, $2, $3 and so on.

USAGE="Usage: myscript.ksh arg1 arg2"
print "The file name is $0"
print "The first parameter is $1"
print "The second parameter is $2"

You will notice that the usage line has now changed. It now has the parameters that must be passed to it. A better way to put the file name in the usage line is with $0 since the file name might be changed.

To check that the correct amount of parameters were entered you can use $#. If the wrong number of parameters were entered then the usage line must be printed.

USAGE="Usage: $0 arg1 arg2"
if (($#==2)) then
   print "The file name is $0"
   print "The first parameter is $1"
   print "The second parameter is $2"
else
   print "$USAGE"
fi

Learn UNIX tutorial
UNIX console
vi editor
KornShell scripting