Previous Section  < Day Day Up >  Next Section

14.1. Introduction

When commands are executed from within a file, instead of from the command line, the file is called a shell script and the shell is running noninteractively. When the bash (Bourne Again) shell starts running noninteractively, it looks for the environment variable, BASH_ENV (ENV) and starts up the file (normally .bashrc) assigned as its value. After the BASH_ENV file has been read, the shell will start executing commands in the script.[1]

[1] When bash starts interactively, if the –norc or ––norc option is given, the BASH_ENV or ENV file will not be read.

14.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, commonly called the shbang line, is written as


#!/bin/bash


The #!, also called the magic number, 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. The bash program can also accept arguments to modify its behavior. See Table 14.8 on page 951 for a list of bash options.

Table 14.8. bash 2.x Shell Invocation Options

Option

Meaning

–c string

Commands are read from string. Any arguments after string are assigned to positional parameters, starting at $0.

–D

A list of double-quoted strings, preceded by a $, are printed to standard output. These strings are subject to language translation when the current locale is not C or POSIX. The –n option is implied; no commands will be executed.

–i

Shell is in the interactive mode. TERM, QUIT, and INTERRUPT are ignored.

–s

Commands are read from standard input and allow the setting of positional parameters.

–r

Starts a restricted shell.

––

Signals the end of options and disables further option processing. Any arguments after –– or are treated as filenames and arguments.

––dump–strings

Same as –D.

––help

Displays a usage message for a built-in command and exits.

––login

Causes bash to be invoked as a login shell.

––noediting

When bash is running interactively, does not use the Readline library.

––noprofile

When starting up, bash does not read the initialization files /etc/profile, ~/.bash_profile, ~/.bash_login, or ~/.profile.

––norc

For interactive shells, bash will not read the ~/.bashrc file. Turned on by default, if running shell as sh.

––posix

Changes the behavior of bash to match the POSIX 1003.2 standard, if otherwise it wouldn't.

––quiet

Displays no information at shell startup, the default.

––rcfile file

If bash is interactive, uses this intialization file instead of ~/.bashrc.

––restricted

Starts a restricted shell.

––verbose

Turns on verbose; same as –v.

––version

Displays version information about this bash shell and exits.


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 bash Shell Constructs

A bash shell program consists of a combination of UNIX/Linux commands, bash 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 14.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 will create a script in the editor. After the user saves 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 14.2.

(The Script)

1   #!/bin/bash

2   # This is the first Bash shell program of the day.

    # Scriptname: greetings

    # Written by:  Barbara Bashful

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)

    $ greetings     # Don't forget to turn turn on x permission!

    bash: ./greetings: Permission denied.

    $ chmod +x greetings

    $ greetings  or  ./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/bash, lets the kernel know what interpreter will execute the lines in this program; in this case, the bash 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.

    Previous Section  < Day Day Up >  Next Section