Ksh preparedness

Howdy, and welcome to the intro to Korn shell programming, AKA POSIX.1 shell.

This is the first part of the larger tutorial. It assumes you want to learn how to really be a programmer in ksh, as opposed to someone who just quickly throws something together in a file and stops as soon as it works.

This particular chapter assumes you have done minimal or no sh programming before, so has a lot more general stuff. Here are the most important things to know and do, before really getting serious about shellscripting.

Step 0. Learn how to type.

No, I mean type PROPERLY, and unconciously. You should be able to type faster than you can write, and not be sweating while doing so. The reason for this is that good programming practices involve extra typing. You won't follow the best practices, if you are too fussed up about "How can I use as few characters as possible to get all this typing done?"

Step 1. Learn how to use a text editor.

No, not microsoft word, or word perfect. Those are "word processors". I'm talking about something small, fast, and quick, that makes shoving pure text around easy. [Note that emacs does not fit either 'small', 'fast', OR 'quick' :-)]

You will have to be very comfortable with your choice of text editor, because thats how you make shellscripts. All examples given should be put into some file. You can then run it with "ksh file".
Or, do the more official way; Put the directions below, exactly as-is, into a file, and follow the directions in it.

#!/bin/ksh
# the above must always be the first line. But generally, lines
# starting with '#' are comments. They dont do anything.
# This is the only time I will put in the '#!/bin/ksh' bit. But
# EVERY EXAMPLE NEEDS IT, unless you want to run the examples with
#  'ksh filename' every time.
#
# If for some odd reason, you dont have ksh in /bin/ksh, change
# the path above, as appropriate.
#
# Then do  'chmod 0755 name-of-this-file'. After that,
# you will be able to use the filename directly like a command

echo Yeup, you got the script to work!

Step 2. Understand variables.

Hopefully, you already understand the concept of a variable. It is a place you can store a value to, and then do operations on "whatever is in this place",vs the value directly.

In shellscripts, a variable can contain a collection of letters and/or numbers [aka a 'string'] , as well as pure numbers.

You set a variable by using

variablename="some string here"
  OR
variablename=1234
You access what is IN a variable, by putting a dollar-sign in front of it.
echo $variablename
  OR
echo ${variablename}
If you have JUST a number in a variable, you can do math operations on it. But that comes later on in this tutorial.

Step 3. Put everything in appropriate variables

Well, okay, not EVERYTHING :-) But properly named variables make the script more easily readable. There isn't really a 'simple' example for this, since it is only "obvious" in large scripts. So either just take my word for it, or stop reading and go somewhere else now!

An example of "proper" variable naming practice:


#Okay, this script doesnt do anything useful, it is just for demo purposes.
# and normally, I would put in more safety checks, but this is a quickie.
INPUTFILE="$1"
USERLIST="$2"
OUTPUTFILE="$3"

count=0

while read username ; do
	grep $username $USERLIST >>$OUTPUTFILE
	count=$(($count+1))
done < $INPUTFILE
echo user count is $count	

While the script may not be totally readable to you yet, I think you'll agree it is a LOT clearer than the following;


i=0
while read line ; do
	grep $line $2 >> $3
	i=$(($i+1))
done <$1
echo $i

Note that '$1' means the first argument to your script.
'$*' means "all the arguments together
'$#' means "how many arguments are there?"

Step 4. Know your quotes

It is very important to know when, and what type, of quotes to use.
Quotes are generally used to group things together into a single entity.

Single-quotes are literal quotes.
Double-quotes can have their contents expanded

 echo "$PWD"
prints out your current directory
 echo '$PWD'
prints out the string $PWD
 echo $PWDplusthis
prints out NOTHING. There is no such variable "PWDplusthis
 echo "$PWD"plusthis
prints out your current directory, and the string "plusthis" immediately following it. You could also accomplish this with the alternate form of using variables,
 echo ${PWD}plusthis

There is also what is sometimes called the `back quote`, or ` backtick`: This is not used to quote things, but actually to evaluate and run commands.


TOP of tutorial
Next: Ksh Basics: Loops and conditions
This material is copyrighted by Philip Brown