linux week7

46
Week 7 Working with the BASH Shell

Upload: michael-belisle

Post on 11-Nov-2014

438 views

Category:

Documents


1 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Linux week7

Week 7Working with the BASH Shell

Page 2: Linux week7

Objectives

Redirect the input and output of a command

Identify and manipulate common shell environment variables

Create and export new shell variables Edit environment files to create variables

upon shell startup

Linux+ Guide to Linux Certification, 2e 2

Page 3: Linux week7

Objectives (continued)

Describe the purpose and nature of shell scripts

Create and execute basic shell scripts Effectively use common decision

constructs in shell scripts

Linux+ Guide to Linux Certification, 2e 3

Page 4: Linux week7

Command Input and Output BASH shell responsible for:

Providing user interface Interpreting commandsManipulating command input and output

○ Provided user specifies certain shell metacharacters with command

File descriptors: Numeric labels that define command input and command output

Linux+ Guide to Linux Certification, 2e 4

Page 5: Linux week7

Command Input and Output (continued) Standard Input (stdin): File descriptor

representing command input Standard Output (stdout): File descriptor

representing command output Standard Error (stderror): File descriptor

representing command error messages

Linux+ Guide to Linux Certification, 2e 5

Page 6: Linux week7

Command Input and Output (continued)

Linux+ Guide to Linux Certification, 2e 6

Figure 7-1: The three common file descriptors

Page 7: Linux week7

Redirection

Redirect stdout and stderr from terminal screen to a file Use “>” shell metacharacterCan redirect stdout and stderr to separate

files

Use separate filenames for stdout and stderr

Linux+ Guide to Linux Certification, 2e 7

Page 8: Linux week7

Redirection (continued)

Redirecting stdin to a file:Use “<“ shell metacharacter

tr command: Replace characters in a file sent via stdin

Linux+ Guide to Linux Certification, 2e 8

Page 9: Linux week7

Redirection (continued)

Linux+ Guide to Linux Certification, 2e 9

Table 7-1: Common redirection examples

Page 10: Linux week7

Pipes

Send stdout of one command to another command as stdin

Pipe: String of commands connected by “|” metacharactersstdout on left, stdin on right

Commonly used to reduce amount of information displayed on terminal screen

Linux+ Guide to Linux Certification, 2e 10

Page 11: Linux week7

Pipes (continued)

11Linux+ Guide to Linux Certification, 2e

Figure 7-2: Piping information from one command to another

Page 12: Linux week7

Pipes (continued)

Can use multiple pipes on command linePass information from one command to

another over a series of commands

filter commands: Commands that can take from stdin and give to stdoutCan be on either side of a pipe

tee commands: Filter commands that also send information to a file

Linux+ Guide to Linux Certification, 2e 12

Page 13: Linux week7

Pipes (continued)

13Linux+ Guide to Linux Certification, 2e

Figure 7-3: Piping several commands

Page 14: Linux week7

Pipes (continued)

Linux+ Guide to Linux Certification, 2e 14

Page 15: Linux week7

Pipes (continued)

Can combine redirection and pipingInput redirection must occur at beginning of

pipe Output redirection must occur at end of pipe

sed filter command: Search for and replace text strings

awk filter command: Search for text and perform specified action on it

Linux+ Guide to Linux Certification, 2e 15

Page 16: Linux week7

Shell Variables Variable: A reserved portion of memory

containing accessible information BASH shell has several variables in

memory Environment variables: Contain information

that system and programs access regularly User-defined variables: Custom variables

define by users Special variables

Useful when executing commands and creating new files and directories

Linux+ Guide to Linux Certification, 2e 16

Page 17: Linux week7

Environment Variables

set command: Lists environment variables and current values

echo command: View contents a specified variableUse $ shell metacharacter

Changing value of a variable:Specify variable name followed by equal

sign (=) and new value

Linux+ Guide to Linux Certification, 2e 17

Page 18: Linux week7

Environment Variables (continued)

Linux+ Guide to Linux Certification, 2e 18

Table 8-3: Common BASH environment variables

Page 19: Linux week7

Environment Variables (continued)

Linux+ Guide to Linux Certification, 2e 19

Table 7-3 (continued): Common BASH environment variables

Page 20: Linux week7

Environment Variables (continued)

Linux+ Guide to Linux Certification, 2e 20

Table 7-3 (continued): Common BASH environment variables

Page 21: Linux week7

User-Defined Variables

Variable identifier: Name of a variable Creating new variables:

Specify variable identifier followed by equal sign and the new contents

Features of variable identifiers:Can contain alphanumeric characters, dash

characters, or underscore charactersMust not start with a numberTypically capitalized to follow convention

Linux+ Guide to Linux Certification, 2e 21

Page 22: Linux week7

User-Defined Variables (continued) Subshell: Shell created by current shell

Most shell commands run in a subshellVariables created in current shell are not

available to subshells export command: Exports user-defined

variables to subshells Ensures that programs started by current

shell have access to variables env command: Lists all exported

environment and user-defined variables in a shell

Linux+ Guide to Linux Certification, 2e 22

Page 23: Linux week7

Other Variables Not displayed by set or env commands

Perform specialized functions in the shelle.g., UMASK variable

alias command: Creates shortcuts to commandsUse unique alias namesAliases stored in special variablesCan create single alias to multiple

commands○ Use ; metacharacter

Linux+ Guide to Linux Certification, 2e 23

Page 24: Linux week7

Environment Files

When exiting BASH shell, all stored variables are destroyed

Environment files: Store variables and valuesExecuted each time BASH shell is startedEnsures variables are always accessible

Linux+ Guide to Linux Certification, 2e 24

Page 25: Linux week7

Environment Files (continued) Common BASH shell environment files

(in order they are executed):/etc/profile~/.bash_profile~/.bash_login~/.profile

Hidden environment files allow users to set customized variables

Linux+ Guide to Linux Certification, 2e 25

Page 26: Linux week7

Environment Files (continued) To add a variable, add a line to

environment fileUse command line syntax

Any command can be placed inside any environment filee.g., alias creation

.bashrc (BASH run-time configuration): First hidden environment file executed at login

Linux+ Guide to Linux Certification, 2e 26

Page 27: Linux week7

Shell Scripts

Shell script: Text file containing a list of commands or constructs for shell to executeMay contain any command that can be

entered on command line

Hashpling: First line in a shell scriptDefines which shell is used to interpret shell

script commands

Linux+ Guide to Linux Certification, 2e 27

Page 28: Linux week7

Shell Scripts (continued)

Executing shell scripts with read permission: Start another BASH shell, specify the shell

script as an argument

Executing shell scripts with read/write permission:Executed like any executable program

Linux+ Guide to Linux Certification, 2e 28

Page 29: Linux week7

Escape Sequences

Character sequences having special meaning in the echo commandPrefixed by \ characterMust use –e option in echo command

Linux+ Guide to Linux Certification, 2e 29

Page 30: Linux week7

Escape Sequences (continued)

Linux+ Guide to Linux Certification, 2e 30

Table 7-4: Common echo escape sequences

Page 31: Linux week7

Reading Standard Input Shell scripts may need input from user

Input may be stored in a variable for later use

read command: Takes user input from stdinPlaces in a variable specified by an

argument to read command

Linux+ Guide to Linux Certification, 2e 31

Page 32: Linux week7

Decision Constructs

Most common type of construct used in shell scripts

Alter flow of a program:Based on whether a command completed

successfullyBased on user input

Linux+ Guide to Linux Certification, 2e 32

Page 33: Linux week7

Decision Constructs (continued)

Linux+ Guide to Linux Certification, 2e 33

Figure 7-4: A sample decision construct

Page 34: Linux week7

Decision Constructs (continued)

Linux+ Guide to Linux Certification, 2e 34

Figure 7-5: A sample decision construct

Page 35: Linux week7

The if Construct

Control flow of program based on true/false decisions

Syntax:

Linux+ Guide to Linux Certification, 2e 35

Page 36: Linux week7

The if Construct (continued) Common rules governing if constructs:

elif (else if) and else statements optionalUnlimited number of elif statementsdo these commands section may consist of

multiple commands○ One per line

do these commands section typically indented for readability

End of statement must be “if”this is true may be a command or test statement

Linux+ Guide to Linux Certification, 2e 36

Page 37: Linux week7

The if Construct (continued) test statement: Used to test a condition

Generates a true/false valueInside of square brackets ( [ … ] )

○ Must have spaces after “[” and before “]”

Special comparison operators: –o (OR) –a (AND)! (NOT)

Linux+ Guide to Linux Certification, 2e 37

Page 38: Linux week7

The if Construct (continued)

Linux+ Guide to Linux Certification, 2e 38

Table 7-5: Common test statements

Page 39: Linux week7

The if Construct (continued)

Linux+ Guide to Linux Certification, 2e 39

Table 7-6: Special operators in test statements

Page 40: Linux week7

The case Construct

Compares value of a variable with several different patterns of text or numbers

Syntax:

Linux+ Guide to Linux Certification, 2e 40

Page 41: Linux week7

The case Construct (continued) If a match is found, commands to right

of pattern are executed Must end with esac

Linux+ Guide to Linux Certification, 2e 41

Page 42: Linux week7

The && and || Constructs

Time-saving shortcut constructsWhen only one decision needs to be made

during execution

Syntax:command && commandcommand || command

Linux+ Guide to Linux Certification, 2e 42

Page 43: Linux week7

The && and || Constructs (continued) &&: Second command executed only if

first completes successfully ||: Second command executed only if

first fails

Linux+ Guide to Linux Certification, 2e 43

Page 44: Linux week7

Summary Three components are available to

commands: Standard Input, Standard Output, and Standard Error

Standard Input is typically user input taken from the keyboard; Standard Output and Standard Error are sent to the terminal screen

You can redirect the Standard Output and Standard Error of a command to a file using redirection symbols

Linux+ Guide to Linux Certification, 2e 44

Page 45: Linux week7

Summary (continued) Use the pipe symbol to redirect the

Standard Output from one command to the Standard Input of another

Most variables available to the BASH shell are environment variables that are loaded into memory after login from environment files

You can create your own variables in the BASH shell and export them so that they are available to programs started by the shell

Linux+ Guide to Linux Certification, 2e 45

Page 46: Linux week7

Summary (continued) The UMASK variable and command

aliases are special variables that must be set using a certain command

Shell scripts can be used to execute several Linux commands

Decision constructs can be used in shell scripts to execute certain Linux commands based on user input or the results of a certain command

Linux+ Guide to Linux Certification, 2e 46