261 264




Linux Unleashed, Third Edition:Shell Programming





-->















Previous
Table of Contents
Next




Chapter 14Shell Programming

by Rick McMullin

In This Chapter
•   Creating and running shell programs
•   Using variables
•   The importance of quotation marks
•   The test command
•   Conditional statements
•   Iteration statements
•   Functions

The last three chapters described how to use the most common Linux shell programs; now let’s take a closer look at the powerful interpretive programming languages that these shell programs have built into them.

This chapter describes the fundamentals of shell programming and compares the bash, pdksh, and tcsh programming languages. This chapter covers the following topics:

•  Creating and running shell programs
•  Using shell variables
•  The importance of quotation marks
•  The test command
•  Conditional statements
•  Iteration statements
•  Functions

This chapter contains several small examples of shell programs. Each new concept or command that is introduced has some example code that further explains what is being presented.

Creating and Running Shell Programs
At the simplest level, shell programs are just files that contain one or more shell or Linux commands. These programs can be used to simplify repetitive tasks, to replace two or more commands that are always executed together with a single command, to automate the installation of other programs, and to write simple interactive applications.

To create a shell program, you must first create a file using a text editor and put the shell or Linux commands you want to be executed into that file. For example, we’ll assume you have a CD-ROM drive mounted on your Linux system. This CD-ROM device is mounted when the system is first started. If you later change the CD in the drive, you must force Linux to read the new directory contents. One way of achieving this is to put the new CD into the drive, unmount the CD-ROM drive using the Linux umount command, and then remount the drive using the Linux mount command. This sequence of steps is shown by the following commands:


umount /dev/cdrom
mount -t iso1960 /dev/cdrom /cdrom


Instead of typing both of these commands each time you change the CD in your drive, you can create a shell program that will execute both of these commands for you. To do this, put the two commands into a file and name the file remount (or any other name you want).
Several ways of executing the commands are contained in the remount file. One way to accomplish this is to make the file executable. This is done by entering the following command:


chmod +x remount


This command changes the permissions of the file so that it is now executable. You can now run your new shell program by typing remount on the command line.

Note:  The remount shell program must be in a directory that is in your search path, or the shell will not be able to find the program to execute. Also, if you are using tcsh to write programs, the first line of the shell program must start with a # for tcsh to recognize it as a tcsh program file.

Another way you can execute the shell program is to run the shell that the program was written for and pass the program in as a parameter to the shell. In a tcsh program, this is done by entering the following command:


tcsh remount


This command starts up a new shell and tells it to execute the commands that are found in the remount file.
A third way of executing the commands in a shell program file is to use the . command (in pdksh and bash) and the source command in tcsh. This command tells the shell to execute all the commands in the file that is passed as an argument to the command. For example, the following command can be used to tell bash or pdksh to execute the commands in the remount file:


. remount


To do the same thing in tcsh, type the following command:


source remount


Another situation in which a simple shell program can save a lot of time is described in the following example. Let’s say that you are working on three different files in a directory, and at the end of every day you want to back up those three files onto a floppy disk. To do this, simply type a series of commands similar to the following:



mount -t msdos /dev/fd0 /a
cp file1 /a
cp file2 /a
cp file3 /a


As stated in the example, one way of doing this is to mount the floppy drive and then type three copy commands, one for each file you want to copy. A simpler way is to put the four commands into a text file called backup and then execute the backup command when you want to copy the three files onto the floppy drive.

Note:  You will still have to ensure that the backup shell program is executable and is in a directory that is in your path before you run the command.

Using Variables
As is the case with almost any language, the use of variables is very important in shell programs. You’ve already seen in the introductory shell chapters some of the ways in which shell variables can be used. Two of the variables introduced were the PATH variable and the PS1 variable. These are examples of built-in shell variables or variables that are defined by the shell program you are using. This section describes how you can create your own variables and use them in simple shell programs.



Previous
Table of Contents
Next














Wyszukiwarka

Podobne podstrony:
261 264
Glee S01E17 Bad Reputation 720p WEB DL DD5 1 h 264 LP(1)
14 (261)
18 (264)
Rozdzial (264)
260 261
255 264
08 (261)
MAKIJAŻ 261 ALEXANDRE HERCHCOVITCH
264 (2)

więcej podobnych podstron