Chapter 20: Bourne-Again Shell (bash)


 Download CD Content

In This Chapter

  • An Introduction to Bash Scripting

  • Scripting versus Interactive Shell Use

  • User Variables and Environmental Variables

  • Arithmetic in Bash

  • Tests, Conditionals, and Loops in Bash

  • Script Input and Output

  • Dissecting of Useful Scripts

Introduction

In this chapter, we ll explore script programming in the Bourne-Again SHell, otherwise known as bash. Bash is the de facto standard command shell and shell scripting language on Linux and other UNIX systems. We ll investigate some of the major language features of bash, including variables, arithmetic, control structures, input and output, and function creation. This follows the flow of all other scripting chapters in this book, allowing the reader to easily understand the similarities and differences of each covered language.

Preliminaries

Before jumping in to scripting with bash, let s look at some preliminaries that are necessary to run bash scripts. You can tell which shell you re currently using by interrogating the SHELL environment variable:

 $ echo $SHELL     /bin/bash     $ 

The echo command is used to print to the screen. We print the contents of the SHELL variable, accessing the variable by preceding it with the $ symbol. The result is the shell that we re currently operating on, in this case, bash. Technically, it printed out the location of the shell we re using (the bash interpreter is found within the /bin subdirectory).

If we happened not to be using bash, we could simply invoke bash to start that interpreter:

 $ echo $SHELL     /bin/csh     $ bash     $ 

In this case, we were using another command shell (C-shell here). We invoke bash to start this interpreter for further commanding .

Sample Script

Let s now write a simple script as a first step to bash scripting. The source for the script is shown in Listing 20.1.

Listing 20.1: First Bash Script (on the CD-ROM at ./source/ch20/first.sh )
start example
 #!/bin/bash     echo "Welcome to $SHELL scripting."     exit 
end example
 

When invoked, this script simply emits the line Welcome to /bin/bash scripting. and then exits. If you tried to enter this script (named  first.sh ) and execute it as ./first.sh , you d notice that it didn t work. You probably saw something like this:

 $ ./first.sh     -bash: ./first.sh: Permission denied.     $ 

The problem here is that the script is not executable. We must first change the attributes of the file to tell GNU/Linux that it can be executed. This is done using the chmod command, as illustrated below:

 $ chmod +x first.sh     $ ./first.sh     Welcome to /bin/bash scripting.     $ 

We use chmod to set the execute attribute of the file  first.sh , telling GNU/Linux that it can be executed. After trying to execute again, we see the expected results.

The question we could ask ourselves now is, even though we ve told GNU/_Linux that the file can be executed, how does it know that the file is a bash script? The answer is the shebang line in our script. You ll notice that the first line of the script starts with #! (also called shabang ) followed by the path and interpreter ( /bin/bash ). This defines that bash is the shell to be used to interpret this file. If the file had contained a Perl script, it would have begun #! /bin/perl . We can also add comments to our script simply by preceding them with a # character.

Since our interpreter is also our shell, we can perform this command interactively, such as:

 $ echo "Welcome to $SHELL scripting."     Welcome to /bin/bash scripting.     $ 

Now that we have some basics under our belt, let s dig into bash and work through some more useful scripts.




GNU/Linux Application Programming
GNU/Linux Application Programming (Programming Series)
ISBN: 1584505680
EAN: 2147483647
Year: 2006
Pages: 203
Authors: M. Tim Jones

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