-->

Previous | Table of Contents | Next

Page 411

CHAPTER 21

Shell Programming

by Sanjiv Guha

IN THIS CHAPTER

Page 412

When you enter commands from the command line, you are entering commands one at a time and getting the response from the system. From time to time you will need to execute more than one command, one after the other, and get the final result. You can do so with a shell program or shell script. A shell program is a series of Linux commands and utilities that have been put into a file using a text editor. When you execute a shell program, each command is interpreted and executed by Linux, one after the other.

You can write shell programs and execute them like any other command under Linux. You can also execute other shell programs from within a shell program if they are in the search path. A shell program is like any other programming language and has its own syntax. You can have variables defined and assign various values and so on. These are discussed in this chapter.

Creating and Executing a Shell Program

Say you want to set up a number of aliases every time you log on. Instead of typing all the aliases every time you log on, you can put them in a file using a text editor, such as vi, and then execute the file.

Here is a list of what is contained in a sample file created for this purpose, myenv:


alias ll `ls _l'

alias dir `ls'

alias copy `cp'

myenv can be executed in a variety of ways under Linux.

You can make myenv executable by using the chmod command as follows, and then execute it as you would any other native Linux command:


chmod +x myenv

This turns on the executable permission of myenv. There is one more thing you need to ensure before you can execute myenv. The file myenv must be in the search path. You can get the search path by executing


echo $PATH

If the directory where the file myenv is located is not in the current search path, then you must add the directory name in the search path.

Now you can execute the file myenv from the command line, as if it were a Linux command:


myenv

Page 413

NOTE
You must ensure that the first line in your shell program starts with a pound sign (#). The pound sign tells the shell that the line is a comment. Following the pound sign, you must have an exclamation point (!), which tells the shell to run the command following the exclamation point and to use the rest of the file as input for that command. This is common practice for all shell scripting.

A second way to execute myenv under a particular shell, such as pdksh, is as follows:


pdksh myenv

This invokes a new pdksh shell and passes the filename myenv as a parameter to execute the file.

You can also execute myenv from the command line as follows:

Command Environment


. myenv                  pdksh and bash







source myenv             tcsh



The dot (.) is a way of telling the shell to execute the file myenv. In this case, you do not have to ensure that execute permission of the file is set. Under tcsh, you have to use the source command instead of the dot (.) command.

After executing the command myenv, you should be able to use dir from the command line to get a list of files under the current directory, and ll to get a list of files with various attributes displayed.

Variables

Linux shell programming is a full-fledged programming language and as such supports various types of variables. There are three major types of variables: environment, built-in, and user.

Environment variables are part of the system environment, and you do not have to define them. You can use them in your shell program; some of them, like PATH, can also be modified within a shell program.

Built-in variables are provided by the system; unlike environment variables, you cannot modify them.

User variables are defined by you when you write a shell script. You can use and modify them at will within the shell program.

Page 414

A major difference between shell programming and other programming languages is that in shell programming, variables are not typecast; that is, you do not have to specify whether a variable is a number or a string and so on.

Assigning a Value to a Variable

Say you want to use a variable called lcount to count the number of iterations in a loop within a shell program. You can declare and initialize this variable as follows:

Command Environment




lcount=0                  pdksh and bash







set lcount =              0tcsh

NOTE
Under pdksh and bash, you must ensure that the equal sign (=) does not have space before and after it.

As shell programming languages do not use typed variables, it is possible that the same variable can be used to store an integer value one time and a string another time. However, this is not recommended. You should be careful not to do this.

To store a string in a variable, you can use the following:

Command Environment


myname=Sanjiv                   pdksh and bash







set myname = Sanjiv             csh

The preceding can be used if the string does not have embedded spaces. If a string has embedded spaces, you can do the assignment as follows:

Command Environment


myname='Sanjiv Guha'                 pdksh and bash







set myname = `Sanjiv Guha'           tcsh



Accessing Variable Values

You can access the value of a variable by prefixing the variable name by a $ (dollar sign). That is, if the variable name is var, you can access the variable by using $var.

Previous | Table of Contents | Next