|
|
We will begin by creating a simple shell program that will do the following tasks, in order:
This is the end of the shell program.
pwd ls echo This is the end of the shell program.Now write and quit the file. You have just created a shell program! You can cat the file to display its contents, as the following screen shows:
$ cat dl pwd ls echo This is the end of the shell program. $
One way to execute a shell program is to use the sh command. Type:
sh dlThe dl command is executed by sh, and the pathname of the current directory is printed first, then the list of files in the current directory, and finally, the comment
This is the end of the shell program
.
The
sh
command provides a good way to test
your shell program to make sure it works.
If dl is a useful command, you can use the chmod command to make it an executable file; then you can type dl by itself to execute the command it contains. The following example shows how to use the chmod command to make a file executable and then run the ls -l command to verify the changes you have made in the permissions.
$ chmod u+x dl $ ls -l total 2 -rw------- 1 login login 3661 Nov 2 10:28 mbox -rwx------ 1 login login 48 Nov 15 10:50 dl $
Notice that chmod turns on permission to execute (+x) for the user (u). Now dl is an executable program. Try to execute it. Type:
dlYou get the same results as before, when you entered sh dl to execute it.
To make your shell programs accessible from all your directories, you can make a bin directory from your login directory and move the shell files to your bin.
You must also set your shell variable PATH to include your bin directory:
PATH=$PATH:$HOME/binSee ``Variables'' and ``Using shell variables'' for more information about PATH.
The following example reminds you which commands are necessary. In this example, dl is in the login directory. Type these command lines:
cd mkdir bin mv dl bin/dlMove to the bin directory and type the ls -l command. Does dl still have execute permission?
Now move to a directory other than the login directory, and type the following command:
dlWhat happened?
It is possible to give the bin directory another name; if you do so, you must change your shell variable PATH again.
You can give your shell program any appropriate filename; however, you should not give your program the same name as a system command. Depending on your path, the system may execute your command instead of the system command. For example, if you had named your dl program mv, each time you tried to move a file, the system might have executed your directory list program instead of mv.
Another problem can occur if you name the dl file ls, and then try to execute the file. You would create an infinite loop, since your program executes the ls command. After some time, the system would give you the following error message:
Too many processes, cannot forkWhat happened? You typed in your new command, ls. The shell read and executed the pwd command. Then it read the ls command in your program and tried to execute your ls command. This formed an infinite loop. For this reason, the UNIX system limits the number of times an infinite loop can execute. One way to prevent such looping is to give the pathname for the system ls command, /usr/bin/ls, when you write your own shell program.
The following ls shell program would work:
$ cat ls pwd /bin/ls echo This is the end of the shell programIf you name your command ls, then you can only execute the system ls command by using its full pathname, /usr/bin/ls.