linux+ guide to linux certification chapter eight working with the bash shell
TRANSCRIPT
![Page 1: Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell](https://reader035.vdocuments.site/reader035/viewer/2022062409/56649ed45503460f94be52f8/html5/thumbnails/1.jpg)
Linux+ Guide to Linux Certification
Chapter Eight
Working with the BASH Shell
![Page 2: Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell](https://reader035.vdocuments.site/reader035/viewer/2022062409/56649ed45503460f94be52f8/html5/thumbnails/2.jpg)
ObjectivesObjectives
• 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• Describe the purpose and nature of shell scripts• Create and execute basic shell scripts• Effectively use common decision constructs in shell
scripts
![Page 3: Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell](https://reader035.vdocuments.site/reader035/viewer/2022062409/56649ed45503460f94be52f8/html5/thumbnails/3.jpg)
Command Input and OutputCommand Input and Output
• The BASH shell is responsible for providing a user interface and interpreting commands entered on the command line
• The BASH shell can also manipulate command input and output, provided the user specifies certain shell metacharacters on the command line alongside the command
• File descriptors– Numeric labels used to define command input and
command output
![Page 4: Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell](https://reader035.vdocuments.site/reader035/viewer/2022062409/56649ed45503460f94be52f8/html5/thumbnails/4.jpg)
Command Input and OutputCommand Input and Output
• There are three file descriptors available to each command that can be manipulated by the BASH shell:– Standard Input (stdin)– Standard Output (stdout)– Standard Error (stderror)
![Page 5: Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell](https://reader035.vdocuments.site/reader035/viewer/2022062409/56649ed45503460f94be52f8/html5/thumbnails/5.jpg)
Command Input and OutputCommand Input and Output
• Standard Input (stdin)– Represent information inputed to a command
during execution
• Standard Output (stdout)– Represents the desired output from a command
• Standard Error (stderror)– Represents any error message generated by a
command
![Page 6: Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell](https://reader035.vdocuments.site/reader035/viewer/2022062409/56649ed45503460f94be52f8/html5/thumbnails/6.jpg)
Command Input and OutputCommand Input and Output
Figure 8-1:The three common file descriptors
![Page 7: Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell](https://reader035.vdocuments.site/reader035/viewer/2022062409/56649ed45503460f94be52f8/html5/thumbnails/7.jpg)
RedirectionRedirection
• The BASH shell can redirect Standard Output and Standard Error from the terminal screen to a file on the filesystem using the “>” shell metacharacter followed by absolute or relative pathname of the file
• You can redirect both Standard Output and Standard Error to separate files at the same time
• It is important to use separate filenames to hold the contents of Standard Output and Standard Error
![Page 8: Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell](https://reader035.vdocuments.site/reader035/viewer/2022062409/56649ed45503460f94be52f8/html5/thumbnails/8.jpg)
RedirectionRedirection
Table 8-1: Common redirection examples
![Page 9: Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell](https://reader035.vdocuments.site/reader035/viewer/2022062409/56649ed45503460f94be52f8/html5/thumbnails/9.jpg)
RedirectionRedirection
Table 8-1 (continued): Common redirection examples
![Page 10: Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell](https://reader035.vdocuments.site/reader035/viewer/2022062409/56649ed45503460f94be52f8/html5/thumbnails/10.jpg)
PipesPipes
• Redirection only occurs from a command to a file and vice versa
• You may also send the Standard Output of one command to another command as Standard Input
• Pipe– A string of commands connected by “|”
metacharacters
![Page 11: Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell](https://reader035.vdocuments.site/reader035/viewer/2022062409/56649ed45503460f94be52f8/html5/thumbnails/11.jpg)
PipesPipes
Figure 8-2: Piping information from one command to another
![Page 12: Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell](https://reader035.vdocuments.site/reader035/viewer/2022062409/56649ed45503460f94be52f8/html5/thumbnails/12.jpg)
PipesPipes
Figure 8-3: Piping several commands
![Page 13: Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell](https://reader035.vdocuments.site/reader035/viewer/2022062409/56649ed45503460f94be52f8/html5/thumbnails/13.jpg)
PipesPipes
Table 8-2: Common filter commands
![Page 14: Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell](https://reader035.vdocuments.site/reader035/viewer/2022062409/56649ed45503460f94be52f8/html5/thumbnails/14.jpg)
Shell VariablesShell Variables
• A BASH shell has several variables in memory at any one time– A variable is simply a reserved portion of memory
containing information that may be accessed
• Most variables in the shell are referred to as environment variables since they are typically set by the system and contain information that the system and programs access regularly
![Page 15: Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell](https://reader035.vdocuments.site/reader035/viewer/2022062409/56649ed45503460f94be52f8/html5/thumbnails/15.jpg)
Shell VariablesShell Variables
• You may create your own custom variables– These variables are called user-defined
variables
• In addition to environment and user-defined variables, there are special variables that are useful when executing commands and creating new files and directories
![Page 16: Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell](https://reader035.vdocuments.site/reader035/viewer/2022062409/56649ed45503460f94be52f8/html5/thumbnails/16.jpg)
Environment VariablesEnvironment Variables
• There are many environment variables
• Environment variables are set by default in the BASH shell
• To see a list of these variables and their current values, you may use the set command
![Page 17: Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell](https://reader035.vdocuments.site/reader035/viewer/2022062409/56649ed45503460f94be52f8/html5/thumbnails/17.jpg)
Environment VariablesEnvironment Variables
Table 8-3: Common BASH environment variables
![Page 18: Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell](https://reader035.vdocuments.site/reader035/viewer/2022062409/56649ed45503460f94be52f8/html5/thumbnails/18.jpg)
Environment VariablesEnvironment Variables
Table 8-3 (continued): Common BASH environment variables
![Page 19: Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell](https://reader035.vdocuments.site/reader035/viewer/2022062409/56649ed45503460f94be52f8/html5/thumbnails/19.jpg)
Environment VariablesEnvironment Variables
Table 8-3 (continued): Common BASH environment variables
![Page 20: Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell](https://reader035.vdocuments.site/reader035/viewer/2022062409/56649ed45503460f94be52f8/html5/thumbnails/20.jpg)
User-Defined VariablesUser-Defined Variables
• Variable identifier– The name of a variable
• When creating new variables, it is important to note the following features of variable identifiers:– They can contain alphanumeric characters, the dash
character, or the underscore character– They must not start with a number– They are typically capitalized to follow convention
![Page 21: Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell](https://reader035.vdocuments.site/reader035/viewer/2022062409/56649ed45503460f94be52f8/html5/thumbnails/21.jpg)
User-Defined VariablesUser-Defined Variables
• Most commands that are run by the shell are run in a separate subshell– The subshell is created by the current shell
– Variables created in the current shell are not available to those subshells and the commands running within them
![Page 22: Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell](https://reader035.vdocuments.site/reader035/viewer/2022062409/56649ed45503460f94be52f8/html5/thumbnails/22.jpg)
Other VariablesOther Variables
• There are other variables that are not displayed by the set or env commands– These variables perform specialized functions in
the shell
• The UMASK variable used earlier in this textbook us an example of a special variable that performs a special function in the BASH shell and must be set by the umask command
![Page 23: Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell](https://reader035.vdocuments.site/reader035/viewer/2022062409/56649ed45503460f94be52f8/html5/thumbnails/23.jpg)
Environment FilesEnvironment Files
• When you exit your BASH shell, all variables stored in memory are destroyed along with the shell itself
• Thus to ensure that variables are accessible to a shell at all times, you must place variables in a file that is executed each time you log in and start a BASH shell– These files are called environment files
![Page 24: Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell](https://reader035.vdocuments.site/reader035/viewer/2022062409/56649ed45503460f94be52f8/html5/thumbnails/24.jpg)
Environment FilesEnvironment Files
• Some common BASH shell environment files and the order they are executed in are listed below:– /etc/profile– ~/.bash_profile– ~/.bash_login– ~/.profile
![Page 25: Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell](https://reader035.vdocuments.site/reader035/viewer/2022062409/56649ed45503460f94be52f8/html5/thumbnails/25.jpg)
Shell ScriptsShell Scripts
• Shell script– Text files that contain a list of commands or
constructs for the shell to execute in order
• Hashpling– The first line in a shell script, which defines the
shell that will be used to interpret the commands in the script file
![Page 26: Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell](https://reader035.vdocuments.site/reader035/viewer/2022062409/56649ed45503460f94be52f8/html5/thumbnails/26.jpg)
Escape SequencesEscape Sequences
• Escape sequences– Character sequences that have special meaning inside the echo
commandTable 8-4: Common echo sequences
![Page 27: Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell](https://reader035.vdocuments.site/reader035/viewer/2022062409/56649ed45503460f94be52f8/html5/thumbnails/27.jpg)
Escape SequencesEscape Sequences
Table 8-4: Common echo sequences
![Page 28: Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell](https://reader035.vdocuments.site/reader035/viewer/2022062409/56649ed45503460f94be52f8/html5/thumbnails/28.jpg)
Reading Standard InputReading Standard Input
• There may be times where a shell script may need input from the user executing the program– This input may then be stored in a variable for
later use
• The read command takes user input from Standard Input and places it in a variable specified by an argument to the read command
![Page 29: Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell](https://reader035.vdocuments.site/reader035/viewer/2022062409/56649ed45503460f94be52f8/html5/thumbnails/29.jpg)
Decision ConstructsDecision Constructs
Figure 8-4:A sample decision construct
![Page 30: Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell](https://reader035.vdocuments.site/reader035/viewer/2022062409/56649ed45503460f94be52f8/html5/thumbnails/30.jpg)
Decision ConstructsDecision Constructs
Figure 8-5:A sample decision construct
![Page 31: Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell](https://reader035.vdocuments.site/reader035/viewer/2022062409/56649ed45503460f94be52f8/html5/thumbnails/31.jpg)
The if ConstructThe if Construct
• Common rules that govern if constructs:– elif (else if) and else statements are optional– You may have an unlimited number of elif
statements– The do these commands section may consist of
multiple commands, one per line
![Page 32: Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell](https://reader035.vdocuments.site/reader035/viewer/2022062409/56649ed45503460f94be52f8/html5/thumbnails/32.jpg)
The if ConstructThe if Construct
• Common rules that govern if constructs (continued):– The do these commands section is typically
indented from the left hand side of the text file for readability, but does not need to be
– The end of the statement must be a backwards “if” fi
– The this is true of the if syntax seen earlier may be a command or a test statement
![Page 33: Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell](https://reader035.vdocuments.site/reader035/viewer/2022062409/56649ed45503460f94be52f8/html5/thumbnails/33.jpg)
The if ConstructThe if Construct
Table 8-5: Common test statements
![Page 34: Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell](https://reader035.vdocuments.site/reader035/viewer/2022062409/56649ed45503460f94be52f8/html5/thumbnails/34.jpg)
The if ConstructThe if Construct
Table 8-5 (continued): Common test statements
![Page 35: Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell](https://reader035.vdocuments.site/reader035/viewer/2022062409/56649ed45503460f94be52f8/html5/thumbnails/35.jpg)
The if ConstructThe if Construct
Table 8-6: Special operators in test statements
![Page 36: Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell](https://reader035.vdocuments.site/reader035/viewer/2022062409/56649ed45503460f94be52f8/html5/thumbnails/36.jpg)
The case ConstructThe case Construct
• The case statement compares the value of a variable with several different patterns of text or numbers
• If there is a match, then the commands to the right of the pattern will be executed
• As with the if construct, the case construct must be ended by a backwards “case” (esac)
![Page 37: Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell](https://reader035.vdocuments.site/reader035/viewer/2022062409/56649ed45503460f94be52f8/html5/thumbnails/37.jpg)
The && and || ConstructsThe && and || Constructs
• Although the if and case constructs are versatile, there are some shortcut constructs that take less time when only one decision needs to be made during the execution of a program– These constructs are && and ||
– The syntax of these constructs is listed below:• command && command
• command || command
![Page 38: Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell](https://reader035.vdocuments.site/reader035/viewer/2022062409/56649ed45503460f94be52f8/html5/thumbnails/38.jpg)
Chapter SummaryChapter Summary
• There are three components available to commands• Standard Input is typically user input taken from the
keyboard• Standard Output and Standard Error are sent to the
terminal screen by default• You may redirect the Standard Output and Standard
Error of a command to a file using redirection symbols
![Page 39: Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell](https://reader035.vdocuments.site/reader035/viewer/2022062409/56649ed45503460f94be52f8/html5/thumbnails/39.jpg)
Chapter SummaryChapter Summary
• To redirect the Standard Output from one command to the Standard Input of another, you must use the pipe symbol
• Most variables available to the BASH shell are environment variables, which are loaded into memory after login from environment files
• You may create your own variables in the BASH shell and export them such that they are available to programs started by the shell
![Page 40: Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell](https://reader035.vdocuments.site/reader035/viewer/2022062409/56649ed45503460f94be52f8/html5/thumbnails/40.jpg)
Chapter SummaryChapter Summary
• 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 may be used within shell scripts to execute certain Linux commands based on user input or the results of a certain command