8.1. Introduction

 <  Day Day Up  >  

Shell programs are usually called shell scripts . Now that you have learned how to use the shell interactively, you are ready to start scripting using what you have learned in addition to the programming constructs provided in this chapter. It doesn't matter whether you are a system administrator, a programmer, or a user . Shell scripts can automate a number of routine tasks to make life a lot easier ”and they are fun to write and use!

8.1.1 The Steps in Creating a Shell Script

A shell script is normally written in an editor and consists of commands interspersed with comments. Comments are preceded by a pound sign ( # ) and consist of text used to document what is going on.

The First Line

The first line at the top left corner of the script will indicate the program that will be executing the lines in the script. This line, known as the shbang line, is commonly written as

 #!/bin/sh 

The #! is called a magic number and is used by the kernel to identify the program that should be interpreting the lines in the script. This line must be the top line of your script.

Comments

Comments are lines preceded by a pound sign and can be on a line by themselves or on a line following a script command. They are used to document your script. It is sometimes difficult to understand what the script is supposed to do if it is not commented. Although comments are important, they are often too sparse or not used at all. Try to get used to commenting what you are doing not only for someone else, but also for yourself. Two days from now you may not recall exactly what you were trying to do.

Executable Statements and Bourne Shell Constructs

A Bourne shell program consists of a combination of UNIX commands, Bourne shell commands, programming constructs, and comments.

Making the Script Executable

When you create a file, it is not given the execute permission. You need this permission to run your script. Use the chmod command to turn on the execute permission.

Example 8.1.
 1   $  chmod +x myscript  2   $  ls -lF  myscript   -rwxr-xr-x     1   ellie   0 Jul   13:00 myscript*  

EXPLANATION

  1. The chmod command is used to turn on the execute permission for the user, group , and others.

  2. The output of the ls command indicates that all users have execute permission on the myscript file. The asterisk at the end of the filename also indicates that this is an executable program.

A Scripting Session

In the following example, the user creates a script in the editor. After saving the file, the execute permissions are turned on, and the script is executed. If there are errors in the program, the shell will respond immediately.

Example 8.2.
 (The Script) 1    #!/bin/sh 2  # Scriptname: greetings   # Written by:  Barbara Born   # This is the first Bourne shell program of the day.  3    echo "Hello $LOGNAME, it's nice talking to you." 4    echo "Your present working directory is `pwd`."      echo "You are working on a machine called `uname -n`."      echo "Here is a list of your files." 5    ls  # List files in the present working directory  6    echo  "Bye for now $LOGNAME. The time is `date +%T`!" (The Command Line) 7    $  chmod +x greetings  $  greetings  3  Hello barbara, it's nice talking to you.  4  Your present working directory is /home/lion/barbara/prog   You are working on a machine called lion.   Here is a list of your files.  5  Afile        cplus   letter    prac   Answerbook   cprog   library   prac1   bourne       joke    notes     perl5  6  Bye for now barbara. The time is 18:05:07!  

EXPLANATION

  1. The first line of the script, #!/bin/sh , lets the kernel know what interpreter will execute the lines in this program, in this case the sh (Bourne shell) interpreter.

  2. The comments are nonexecutable lines preceded by a pound sign. They can be on a line by themselves or appended to a line after a command.

  3. After variable substitution is performed by the shell, the echo command displays the line on the screen.

  4. After command substitution is performed by the shell, the echo command displays the line on the screen.

  5. The ls command is executed. The comment will be ignored by the shell.

  6. The echo command displays the string enclosed within double quotes. Variables and command substitution (backquotes) are expanded when placed within double quotes. In this case, the quotes were really not necessary.

  7. The greetings script is given permission to execute by the user, group, and others, and is run from the command line.

 <  Day Day Up  >  


UNIX Shells by Example
UNIX Shells by Example (4th Edition)
ISBN: 013147572X
EAN: 2147483647
Year: 2004
Pages: 454
Authors: Ellie Quigley

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net