9.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 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, in the -norc or --norc option is given, the BASH_ENV or ENV file will not be read.

9.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 #! 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. The bash program can also accept arguments to modify its behavior. See "Bash Options" on page 474 for a list of bash options.

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 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 9.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 joker 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 9.2.
(The Script)
1 #!/bin/bash2# 
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 "our 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 (Bourne Again 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 (back quotes) are expanded when placed within double quotes. In this case, the quotes were really not necessary.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.24.30