470 likes | 936 Views
Linux Guide to Linux Certification, 3e. 2. Objectives. Redirect the input and output of a commandIdentify and manipulate common shell environment variablesCreate and export new shell variablesEdit environment files to create variables upon shell startup. Linux Guide to Linux Certification, 3e.
E N D
1. Linux+ Guide to Linux Certification, Third Edition Chapter 7
Working with the BASH Shell
2. Linux+ Guide to Linux Certification, 3e 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
3. Linux+ Guide to Linux Certification, 3e 3 Objectives (continued) Describe the purpose and nature of shell scripts
Create and execute basic shell scripts
Effectively use common decision constructs in shell scripts
4. Command Input and Output BASH shell responsible for:
Providing user interface
Interpreting commands
Manipulating command input and output
Provided user specifies certain shell metacharacters alongside command
File descriptors: numeric labels that represent command input and command output Linux+ Guide to Linux Certification, 3e 4
5. Linux+ Guide to Linux Certification, 3e 5 Command Input and Output (continued) Standard input (stdin): file descriptor referring to information processed by the command during execution
Often user input typed on keyboard
Standard output (stdout): file descriptor referring to normal output of the command
Displayed on terminal
Standard error (stderror): file descriptor referring to error messages generated by the command
Displayed on terminal
6. Linux+ Guide to Linux Certification, 3e 6 Command Input and Output (continued)
7. Linux+ Guide to Linux Certification, 3e 7 Redirection Redirect stdout and stderr from terminal screen to a file
Use “>” shell metacharacter
Can redirect stdout and stderr to separate files using file descriptor numbers
Use separate filenames for stdout and stderr
The file is automatically created if it did not exist prior to redirection, and is overwritten if it did exist
Use “>>” to append additional output to an existing file
8. Linux+ Guide to Linux Certification, 3e 8 Redirection (continued) Redirecting stdin to a file:
Use “<“ shell metacharacter
tr command: Replace characters in a file sent via stdin
Can redirect stdin and stdout for the same command using “<” and “>“
Use different file names
9. Linux+ Guide to Linux Certification, 3e 9 Redirection (continued)
10. Linux+ Guide to Linux Certification, 3e 10 Pipes Send stdout of one command to another command as stdin
Pipe: String of commands connected by “|” metacharacters
stdout on left, stdin on right
Commonly used to reduce amount of information displayed on terminal screen
11. Linux+ Guide to Linux Certification, 3e 11 Pipes (continued)
12. Linux+ Guide to Linux Certification, 3e 12 Pipes (continued) Can use multiple pipes on command line
Pass information from one command to another over a series of commands
Filter commands: Commands that can take from stdin and give to stdout
Can be on either side of a pipe
tee command: Filter command that receives information from stdin and sends that information to a file, as well as to stdout
13. Linux+ Guide to Linux Certification, 3e 13 Pipes (continued)
14. Linux+ Guide to Linux Certification, 3e 14 Pipes (continued)
15. Linux+ Guide to Linux Certification, 3e 15 Pipes (continued) Can combine redirection and piping
Input redirection must occur at beginning of pipe
Output redirection must occur at end of pipe
sed command: filter command used to search for and replace text strings
Can be used to replace some or all appearances of the search term and to delete specific input lines
awk command: filter command used to search for text and perform specified action on it
Use -F option to set delimiters
16. Linux+ Guide to Linux Certification, 3e 16 Shell Variables Variable: 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
17. Linux+ Guide to Linux Certification, 3e 17 Environment Variables set command: lists environment variables and current values
echo command: view contents of a specified variable
Prefix variable name with the $ shell metacharacter
Changing value of a variable:
Specify variable name followed by equal sign (=) and new value
HOME and PWD variables should not be changed
18. Linux+ Guide to Linux Certification, 3e 18 Environment Variables (continued)
19. Linux+ Guide to Linux Certification, 3e 19 Environment Variables (continued)
20. Linux+ Guide to Linux Certification, 3e 20 Environment Variables (continued)
21. Linux+ Guide to Linux Certification, 3e 21 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 characters
Must not start with a number
Typically capitalized to follow convention
22. Linux+ Guide to Linux Certification, 3e 22 User-Defined Variables (continued) Subshell: shell created by current shell
Most shell commands run in a subshell
Variables 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
23. Linux+ Guide to Linux Certification, 3e 23 Other Variables Not displayed by set or env commands
Perform specialized functions in the shell
e.g., UMASK variable
alias command: creates shortcuts to commands
Use unique alias names
Aliases stored in special variables
Can create single alias to multiple commands which will be executed in sequence
Use ; metacharacter
24. Linux+ Guide to Linux Certification, 3e 24 Environment Files When exiting BASH shell, all stored variables are destroyed
Environment files: store variables and values
Executed each time BASH shell is started
Ensures variables are always accessible
25. Linux+ Guide to Linux Certification, 3e 25 Environment Files (continued) Common BASH shell environment files (in order in which they are executed):
~/.bashrc
/etc/profile
~/.bash_profile
~/.bash_login
~/.profile
Hidden environment files allow users to set customized variables
26. Linux+ Guide to Linux Certification, 3e 26 Environment Files (continued) To add a variable, add a line to environment file
Use command line syntax
Any command can be placed inside any environment file
e.g., alias creation
.bashrc (BASH run-time configuration): first hidden environment file executed at login
27. Linux+ Guide to Linux Certification, 3e 27 Shell Scripts Shell script: text file containing a list of commands or constructs for shell to execute
May contain any command that can be entered on command line
Hashpling: first line in a shell script
Specifies which shell is used to interpret shell script commands
28. Linux+ Guide to Linux Certification, 3e 28 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/execute permission:
Executed like any executable program
Use echo command to add descriptions and spaces to script output
29. Linux+ Guide to Linux Certification, 3e 29 Escape Sequences Character sequences having special meaning in the echo command
Prefixed by \ character
Must use –e option in echo command
30. Linux+ Guide to Linux Certification, 3e 30 Escape Sequences (continued)
31. Linux+ Guide to Linux Certification, 3e 31 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 stdin and places it in a variable
Variable name specified by an argument to read command
32. Linux+ Guide to Linux Certification, 3e 32 Decision Constructs Most common type of construct used in shell scripts
Alter flow of a program:
Based on whether a command completed successfully
Based on user input
33. Linux+ Guide to Linux Certification, 3e 33 Decision Constructs (continued)
34. Linux+ Guide to Linux Certification, 3e 34 Decision Constructs (continued)
35. Linux+ Guide to Linux Certification, 3e 35 The if Construct Control flow of program based on true/false decisions
Most common type of decision construct
Syntax:
36. Linux+ Guide to Linux Certification, 3e 36 The if Construct (continued) Common rules governing if constructs:
elif (else if) and else statements are optional
Unlimited number of elif statements
“Do these commands” section may consist of multiple commands
One per line
Typically indented for readability
End of statement must be fi
“Condition” (this is true portion) may be a command or test statement
37. Linux+ Guide to Linux Certification, 3e 37 The if Construct (continued) test statement: used to test a condition
Generates a true/false value
Inside of square brackets ( [ … ] ) or prefixed by the word “test”
Must have spaces after “[” and before “]”
Special comparison operators: used to combine test statements:
–o (OR)
–a (AND)
! (NOT)
38. Linux+ Guide to Linux Certification, 3e 38 The if Construct (continued)
39. Linux+ Guide to Linux Certification, 3e 39 The if Construct (continued)
40. Linux+ Guide to Linux Certification, 3e 40 The case Construct Compares value of a variable with several different patterns of text or numbers
Syntax:
41. Linux+ Guide to Linux Certification, 3e 41 The case Construct (continued) If a match is found, commands to right of pattern are executed
Must end with esac
42. Linux+ Guide to Linux Certification, 3e 42 The && and || Constructs Time-saving shortcut constructs
When only one decision needs to be made during execution
Syntax:
command && command
command || command
&&: Second command executed only if first completes successfully
||: Second command executed only if first fails
43. Linux+ Guide to Linux Certification, 3e 43 Loop Constructs Loop constructs: execute commands repetitively
Alter the flow of a program based on the results of a particular statement
Repeat entire program until reach desired result
44. The for Construct Can be used to process a list of objects
Syntax:
for var_name in string1 string2 … …
do
these commands
done
During execution sets var_name to a string name, and executes the commands between do and done for that string
Repeats for all the strings Linux+ Guide to Linux Certification, 3e 44
45. The while Construct Begins with test statement
Commands within the loop construct are executed as long as the test statement returns true
Contains a counter variable
Value changes with each iteration of the loop
Syntax:
while this returns true
do
these commands
done Linux+ Guide to Linux Certification, 3e 45
46. Linux+ Guide to Linux Certification, 3e 46 Summary Three components are available to commands: standard input, standard output, and standard error
Stdin is typically user input taken from the keyboard; Stdout and Stderr are sent to the terminal screen
You can redirect the stdout and stderr of a command to a file using redirection symbols
47. Summary (continued) Use the pipe (|) symbol to redirect stdout from one command to the stdin 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, 3e 47
48. 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
Loop constructs can be used within shell scripts to execute a series of commands repetitively Linux+ Guide to Linux Certification, 3e 48