Presentation is loading. Please wait.

Presentation is loading. Please wait.

Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell.

Similar presentations


Presentation on theme: "Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell."— Presentation transcript:

1 Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell

2 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 Describe the purpose and nature of shell scripts Create and execute basic shell scripts Effectively use common decision constructs in shell scripts

3 Command 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

4 Command 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)

5 Command 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

6 Command Input and Output Figure 8-1: The three common file descriptors

7 Redirection 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

8 Redirection Table 8-1: Common redirection examples

9 Redirection Table 8-1 (continued): Common redirection examples

10 Pipes 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

11 Pipes Figure 8-2: Piping information from one command to another

12 Pipes Figure 8-3: Piping several commands

13 Pipes Table 8-2: Common filter commands

14 Shell 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

15 Shell 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

16 Environment 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

17 Environment Variables Table 8-3: Common BASH environment variables

18 Environment Variables Table 8-3 (continued): Common BASH environment variables

19 Environment Variables Table 8-3 (continued): Common BASH environment variables

20 User-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

21 User-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

22 Other 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

23 Environment 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

24 Environment Files Some common BASH shell environment files and the order they are executed in are listed below: –/etc/profile –~/.bash_profile –~/.bash_login –~/.profile

25 Shell 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

26 Escape Sequences Escape sequences –Character sequences that have special meaning inside the echo command Table 8-4: Common echo sequences

27 Escape Sequences Table 8-4: Common echo sequences

28 Reading 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

29 Decision Constructs Figure 8-4: A sample decision construct

30 Decision Constructs Figure 8-5: A sample decision construct

31 The 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

32 The 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

33 The if Construct Table 8-5: Common test statements

34 The if Construct Table 8-5 (continued): Common test statements

35 The if Construct Table 8-6: Special operators in test statements

36 The 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)

37 The && 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

38 Chapter 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

39 Chapter 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

40 Chapter 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


Download ppt "Linux+ Guide to Linux Certification Chapter Eight Working with the BASH Shell."

Similar presentations


Ads by Google