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.
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!
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
You access what is IN a variable, by putting a dollar-sign in front of it.variablename="some string here" OR variablename=1234
If you have JUST a number in a variable, you can do math operations on it. But that comes later on in this tutorial.echo $variablename OR echo ${variablename}
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?"
Single-quotes are literal quotes.
Double-quotes can have their contents expanded
prints out your current directoryecho "$PWD"
prints out the string $PWDecho '$PWD'
prints out NOTHING. There is no such variable "PWDplusthisecho $PWDplusthis
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
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.