Good evening/morning/day/whatever time of day it is to you at the time of reading this blog. I am your instructor for now, MahFreenAmeh, or, as many people know me by, That Crazy Gay, or rather, Gary.. This will be a base class on Linux, as I have seen them done. However, this goes over the basics of programming in ksh (Korn SHell). If you're using WIndows, you can get the ksh emulator, U/WIN, to test these out as you do them.
So, let's start.The fundamentals of ksh programming are much like Batch Programming in DOS. They are a collection of commands use to automate a task, or do something else. A base overview of some of the useful commands follows.First, we have the <b>IF</b> block, which takes the shape of:if [test] then[block statement]fiTest will be what you are testing, such as, if a<1 …Block statement is what you want it to execute, and fi is the closing delimiter. You can think of then and fi as { } in other programming languages, such as [programming language of your choice]Next, <b>FOR</b>While there is the arithmetic for that we are all accustomed too, there is the compoud list for that I use typically in ksh. It goes like:for [var] in- do
[stuff]
done
Where var is the name of the var you're using, list is the list of values you're using, and stuff is what to do. For instance…
for a in 1 2 3 4 5 6 7 8 9 0
do
print – $a
done
This would print 1 2 3 4 5 6 7 8 9 0, on different lines of course. This is NOT working with arithmetic, it is modifying the value of a as a string. I will cover for in arithmetic loops at a later date, along with while, do while, and do until loops.
For passing parameters through a program, so to speak, you use this notation…
EXAMPLE PROGRAM, param
print $1 $2 $3 $4 $5
Now, if you run this line from the command prompt:
param Hello To The Shitty World!
it would print, "Hello To The Shitty World!', without quotation marks. Just as well, if the total amount of parameters you pass is greater than the amount it allows, then it ignores the extra parameters. And note, if your ksh doesn't use print, then replace print with echo.
The <b>ECHO</b> command.
This command accepts some options, which are advanced parameters controlling output. These options are -n, which drops the
at the end of the string, which makes a trailing newline. The option, -e, will escape any escaped sequences of the following varities: v, for vertical tab, for horizontal tab, f for a form feed… If you are really curious, type in "man echo" at the command line without the quotes to see all you can use. Note that if you want to escape characters, they have to be stored between " and ".
Useful Commands!
When it comes to working with directories in shell scripts, an oft-used command is ls, to run the List Service, or basically, list all the files in the current directory. Another useful one is cd, which changes the directory. There are shorthand notations to it. All folders contain two hidden files, ., and .., which are special. If you put cd ., ir will change the directory to the CURRENT dir, that's what the single . stands for, and if you put cd .., it goes up a directory, to the previous one. Just as well, thereis also cd ~/, which changes to the home directory.
Finally, there is the wildcard usage. For example, ls a*a will list everything that starts and ends with an A. Just as well, there is QUESTION MARK expansion for instance, ls ???, which returns all three-letter file names. FInally, there is the pattern matching, such as ls [a-z]* wll list everything that starts with a lower-case letter from a to z, then is followed by anything. There are multiple possible combinations for the pattern, such as [a-z]. [abcde]. [a-n], [0-9], [:alpha:], [:digitL], et cetera. Experiment to your heart's content!
I'll leave here with a quick program to remove all file names with a certain file extemsion on them in the current directory.
for fname in *
do
rm -f *.$1
done
In that, you would have to save it as whatever ou want, then run it from the command prompt, and pass one argument to it: The file extension. For instance, assume it is named prog. If I typed prog txt, it would delete ALL text files in my current directory.
I shall make another one of these sorts of tutorials soon, when I feel like it.
I might cover more advanced topics.
Or, my next tutorial, as they are called, might be on computer hardware, as I have been studying for my A+ certification exam.
Nice.
Eh…your point? ^_^;
Is that Linux rocks.
Heh, interesting.