Shell Programming,continued...
How to Create and Run a Script
Why are we starting with shell script programming? The
reason is that they are easy. Honest, they *are* easy. So easy,
there are several ways to make them.
First, let's walk though the Pico way to create a simple
script.
1) Open an editor program. We'll use the easiest one: Pico.
At the prompt in your shell account, simply type in "pico
hackphile." ("Hackfile" will be the name of the
script you will create. If you don't like that name, open Pico
with the name you like, for example "pico myfilename.")
This brings up a screen that looks a lot like the Pine
email program's "compose mail" screen.
********************************************************
Evil genius tip: If your shell account
is half-way decent, you will have Pine and it will allow you
to choose whatever editor you want for composing email. Default
is Pico. But you may configure it to use other editors such as
the far more powerful vi or emacs. Just go to the main menu on
Pine, then to Setup, then to Configure, then scroll down almost
to the end of all the
options. There will be a line "editor = pico." Put
in your favorite editor! If you regularly use Pine to compose
email, you will keep in practice by using its editor, making
it much easier to write programs.
********************************************************
Here's what your Pico screen should look like:
UW PICO(tm) 2.9
File: hackphile
[ New file ]
^G Get Help ^O WriteOut ^R Read File ^Y Prev Pg
^K Cut Text ^C Cur Pos
^X Exit ^J Justify
^W Where is ^V Next Pg ^U UnCut Text^T To Spell
At the bottom is some fast help, a list of commonly
used Pico commands. That "^" thingy means to hold down
the control key while hitting the letter of the alphabet that
follows. Besides these commands, some others that it helps to
know for Pico are:
^e moves the cursor to the end of a line
^a moves the cursor to the beginning of a line
^d deletes a character
^f moves the cursor forward (or use the -> arrow key if it
works)
^b moves the cursor backward (or use the <- arrow key if it
works)
^p moves the cursor up (or use the up arrow key if it works)
^n moves the cursor down (or use the down arrow key if it works)
^t checks spelling
2) Write in some Unix commands. Here are some fun ones:
echo I am a programmer and one heck of a hacker!
echo Today I am going to
echo $1 $2 $3 $4 $5 $6 $7 $8 $9
3) Now exit Pico. Hold down the control key while pressing
"x." Pico will ask you if you want to save the file.
Hit the "y" key to save. It will ask you whether you
want to save it with the name "hackphile." Unless your
change your mind, just hit the "enter" key and you
are done.
4) Next make it executable. On most systems, you can do this
by typing "chmod 700 hackphile." On some computers
the command "chmod +x hackphile" will work. On other
computers you might have to write a line in your shell script
"#!/bin/bash" (or "#!/bin/tcsh" or "#!/bin/csh"
etc. depending on the path to whatever shell you are using) to
make it work. Sorry to be so
complicated on this instruction, but there are a lot of different
kinds of Unix and Unix shells out there. Groan.
******************************************************
Newbie note: That "chmod" command sets permissions.
Making a file executable is only one of the many things that
magical command does. It also controls who can execute it, who
can read it, and who can write it. Damian Bates of Rt66 Internet
points out that you could set the permissions so only you could
execute that shell script by typing "chmod u+rx filename"
(u=you). If you are in a Unix "group," you could allow
your group to execute it by typing "chmod g+rx filename"
(g=group) or you could give everyone else execute permissions
by typing "chmod o+rx filename" (o=other). Any
of these can be done in combination such as "chmod ug+rx
filename (user and group can read and execute but not write)
or "chmod g-rwx filename" If you hate typing all that
stuff, you can use numbers as in "chmod 700,"
which gives you, and only you read, write and execute ermission.
To add permission to read and execute, but not write, to everyone
else, use "chmod 755." To learn more on how to use
the number chmod commands, use the command "man chmod."
*******************************************************
5) Now type in: "hackphile forge email from Santa Claus."
Press "enter" and you will see on your screen: "I
am a programmer and one heck of a hacker! Today I am going to
forge email from Santa Claus."
Pretty cool, huh? What that last echo command does is
find the first word you typed after the "hackphile"
command, which is held in the memory location $1, the second
word in $2, and so on. Unlike more sophisticated programming
languages, you don't need to set up those dollar sign variables
in advance -- the stuff you type on the command line after the
name of the script automatically goes into those memory locations!
Now suppose you want a script to actually forge email
from Santa Claus. Unfortunately, this is where you learn the
limitations of shell scripts. You can put in the command "telnet
foobar.com 25" and be ready to forge email. But if the next
command in your shell script is "mail from: santa@north.pole.com,"
it just won't happen. The problem is that you are no longer in
your Unix shell. You now are running a mail program on foobar.com,
which does not bring up the rest in your sequence of shell commands.
But help is on the way. The programming languages of
Perl and C will do the job for you much more easily than a shell
script. More on these in later Guides, I promise!
How about more fun ways to make shell scripts?
More shell programming --->>