SlideShare ist ein Scribd-Unternehmen logo
1 von 46
Week 7
Working with the BASH Shell
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
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
Command Input and Output
   BASH shell responsible for:
    Providing user interface
    Interpreting commands
    Manipulating 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
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
Command Input and Output
(continued)




     Figure 7-1: The three common file descriptors


                   Linux+ Guide to Linux Certification, 2e   6
Redirection
   Redirect stdout and stderr from terminal
    screen to a file
    Use “>” shell metacharacter
    Can redirect stdout and stderr to separate
     files
   Use separate filenames for stdout and
    stderr




                     Linux+ Guide to Linux Certification, 2e   7
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
Redirection (continued)




      Table 7-1: Common redirection examples
                 Linux+ Guide to Linux Certification, 2e   9
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



                      Linux+ Guide to Linux Certification, 2e   10
Pipes (continued)




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




                      Linux+ Guide to Linux Certification, 2e   11
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 commands: Filter commands that
    also send information to a file


                     Linux+ Guide to Linux Certification, 2e   12
Pipes (continued)




       Figure 7-3: Piping several commands




                Linux+ Guide to Linux Certification, 2e   13
Pipes (continued)




            Linux+ Guide to Linux Certification, 2e   14
Pipes (continued)
   Can combine redirection and piping
    Input 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
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
Environment Variables
 set command: Lists environment
  variables and current values
 echo command: View contents a
  specified variable
    Use $ shell metacharacter
   Changing value of a variable:
    Specify variable name followed by equal
     sign (=) and new value



                    Linux+ Guide to Linux Certification, 2e   17
Environment Variables
(continued)




    Table 8-3: Common BASH environment variables




                  Linux+ Guide to Linux Certification, 2e   18
Environment Variables
(continued)




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




                     Linux+ Guide to Linux Certification, 2e   19
Environment Variables
(continued)




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




                     Linux+ Guide to Linux Certification, 2e   20
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


                     Linux+ Guide to Linux Certification, 2e   21
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

                     Linux+ Guide to Linux Certification, 2e   22
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
      ○ Use ; metacharacter


                     Linux+ Guide to Linux Certification, 2e   23
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




                    Linux+ Guide to Linux Certification, 2e   24
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
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

                      Linux+ Guide to Linux Certification, 2e   26
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
    Defines which shell is used to interpret shell
      script commands



                     Linux+ Guide to Linux Certification, 2e   27
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
Escape Sequences
   Character sequences having special
    meaning in the echo command
    Prefixed by  character
    Must use –e option in echo command




                   Linux+ Guide to Linux Certification, 2e   29
Escape Sequences (continued)




     Table 7-4: Common echo escape sequences



                 Linux+ Guide to Linux Certification, 2e   30
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
    Places in a variable specified by an
     argument to read command




                     Linux+ Guide to Linux Certification, 2e   31
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




                   Linux+ Guide to Linux Certification, 2e   32
Decision Constructs (continued)




       Figure 7-4: A sample decision construct

                  Linux+ Guide to Linux Certification, 2e   33
Decision Constructs (continued)




      Figure 7-5: A sample decision construct


                 Linux+ Guide to Linux Certification, 2e   34
The if Construct
 Control flow of program based on true/false
  decisions
 Syntax:




          Linux+ Guide to Linux Certification, 2e   35
The if Construct (continued)
   Common rules governing if constructs:
    elif (else if) and else statements optional
    Unlimited number of elif statements
    do 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
The if Construct (continued)
   test statement: Used to test a condition
    Generates a true/false value
    Inside of square brackets ( [ … ] )
      ○ Must have spaces after “[” and before “]”
   Special comparison operators:
    –o (OR)
    –a (AND)
    ! (NOT)



                       Linux+ Guide to Linux Certification, 2e   37
The if Construct (continued)




       Table 7-5: Common test statements


               Linux+ Guide to Linux Certification, 2e   38
The if Construct (continued)




    Table 7-6: Special operators in test statements




                   Linux+ Guide to Linux Certification, 2e   39
The case Construct
 Compares value of a variable with several
  different patterns of text or numbers
 Syntax:




          Linux+ Guide to Linux Certification, 2e   40
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
The && and || Constructs
   Time-saving shortcut constructs
    When only one decision needs to be made
     during execution
   Syntax:
    command && command
    command || command




                    Linux+ Guide to Linux Certification, 2e   42
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
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
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
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

Weitere ähnliche Inhalte

Was ist angesagt?

Linux Administration
Linux AdministrationLinux Administration
Linux Administrationharirxg
 
Chapter09 -- networking with unix and linux
Chapter09  -- networking with unix and linuxChapter09  -- networking with unix and linux
Chapter09 -- networking with unix and linuxRaja Waseem Akhtar
 
LINUX System Call Quick Reference
LINUX System Call Quick ReferenceLINUX System Call Quick Reference
LINUX System Call Quick Referencewensheng wei
 
Lavigne bsdmag july
Lavigne bsdmag julyLavigne bsdmag july
Lavigne bsdmag julyDru Lavigne
 
Unix Linux Commands Presentation 2013
Unix Linux Commands Presentation 2013Unix Linux Commands Presentation 2013
Unix Linux Commands Presentation 2013Wave Digitech
 
02 t1 s2_linux_lesson2
02 t1 s2_linux_lesson202 t1 s2_linux_lesson2
02 t1 s2_linux_lesson2Niit Care
 
Linux Administration
Linux AdministrationLinux Administration
Linux AdministrationHarish1983
 
Unix/Linux Basic Commands and Shell Script
Unix/Linux Basic Commands and Shell ScriptUnix/Linux Basic Commands and Shell Script
Unix/Linux Basic Commands and Shell Scriptsbmguys
 
Linux Command Suumary
Linux Command SuumaryLinux Command Suumary
Linux Command Suumarymentorsnet
 
How To Add System Call In Ubuntu OS
How To Add System Call In Ubuntu OSHow To Add System Call In Ubuntu OS
How To Add System Call In Ubuntu OSPratik Tambekar
 
Programming Embedded linux
Programming Embedded linuxProgramming Embedded linux
Programming Embedded linuxLiran Ben Haim
 
Red hat linux essentials
Red hat linux essentialsRed hat linux essentials
Red hat linux essentialsHaitham Raik
 

Was ist angesagt? (20)

Linux
LinuxLinux
Linux
 
Linux commands
Linux commandsLinux commands
Linux commands
 
Ch02
Ch02Ch02
Ch02
 
Linux Administration
Linux AdministrationLinux Administration
Linux Administration
 
Chapter09 -- networking with unix and linux
Chapter09  -- networking with unix and linuxChapter09  -- networking with unix and linux
Chapter09 -- networking with unix and linux
 
LINUX System Call Quick Reference
LINUX System Call Quick ReferenceLINUX System Call Quick Reference
LINUX System Call Quick Reference
 
Lavigne bsdmag july
Lavigne bsdmag julyLavigne bsdmag july
Lavigne bsdmag july
 
Unix Linux Commands Presentation 2013
Unix Linux Commands Presentation 2013Unix Linux Commands Presentation 2013
Unix Linux Commands Presentation 2013
 
Commands
CommandsCommands
Commands
 
02 t1 s2_linux_lesson2
02 t1 s2_linux_lesson202 t1 s2_linux_lesson2
02 t1 s2_linux_lesson2
 
Linux Administration
Linux AdministrationLinux Administration
Linux Administration
 
Linuxbasiccommands
LinuxbasiccommandsLinuxbasiccommands
Linuxbasiccommands
 
Linux Commands
Linux CommandsLinux Commands
Linux Commands
 
Unix/Linux Basic Commands and Shell Script
Unix/Linux Basic Commands and Shell ScriptUnix/Linux Basic Commands and Shell Script
Unix/Linux Basic Commands and Shell Script
 
Linux commands
Linux commandsLinux commands
Linux commands
 
Linux Command Suumary
Linux Command SuumaryLinux Command Suumary
Linux Command Suumary
 
Ch1 linux basics
Ch1 linux basicsCh1 linux basics
Ch1 linux basics
 
How To Add System Call In Ubuntu OS
How To Add System Call In Ubuntu OSHow To Add System Call In Ubuntu OS
How To Add System Call In Ubuntu OS
 
Programming Embedded linux
Programming Embedded linuxProgramming Embedded linux
Programming Embedded linux
 
Red hat linux essentials
Red hat linux essentialsRed hat linux essentials
Red hat linux essentials
 

Ähnlich wie Linux week7

intro unix/linux 02
intro unix/linux 02intro unix/linux 02
intro unix/linux 02duquoi
 
Introduction 2 linux
Introduction 2 linuxIntroduction 2 linux
Introduction 2 linuxPapu Kumar
 
Shell tutorial
Shell tutorialShell tutorial
Shell tutorialVu Duy Tu
 
Performance Teting - VU Scripting Using Rational (http://www.geektester.blogs...
Performance Teting - VU Scripting Using Rational (http://www.geektester.blogs...Performance Teting - VU Scripting Using Rational (http://www.geektester.blogs...
Performance Teting - VU Scripting Using Rational (http://www.geektester.blogs...raj.kamal13
 
LOSS_C11- Programming Linux 20221006.pdf
LOSS_C11- Programming Linux 20221006.pdfLOSS_C11- Programming Linux 20221006.pdf
LOSS_C11- Programming Linux 20221006.pdfThninh2
 
POS 433 Effective Communication - tutorialrank.com
POS 433 Effective Communication - tutorialrank.comPOS 433 Effective Communication - tutorialrank.com
POS 433 Effective Communication - tutorialrank.comBartholomew59
 
Tool Development 08 - Windows Command Prompt
Tool Development 08 - Windows Command PromptTool Development 08 - Windows Command Prompt
Tool Development 08 - Windows Command PromptNick Pruehs
 
Shell-Scripting-1.pdf
Shell-Scripting-1.pdfShell-Scripting-1.pdf
Shell-Scripting-1.pdfaznabi
 
101 3.1 gnu and unix commands
101 3.1 gnu and unix commands101 3.1 gnu and unix commands
101 3.1 gnu and unix commandsAcácio Oliveira
 
PowerShell Workshop Series: Session 2
PowerShell Workshop Series: Session 2PowerShell Workshop Series: Session 2
PowerShell Workshop Series: Session 2Bryan Cafferky
 

Ähnlich wie Linux week7 (20)

Ch03
Ch03Ch03
Ch03
 
cisco
ciscocisco
cisco
 
Session3
Session3Session3
Session3
 
intro unix/linux 02
intro unix/linux 02intro unix/linux 02
intro unix/linux 02
 
3. intro
3. intro3. intro
3. intro
 
Introduction 2 linux
Introduction 2 linuxIntroduction 2 linux
Introduction 2 linux
 
Chap06
Chap06Chap06
Chap06
 
60761 linux
60761 linux60761 linux
60761 linux
 
Shell tutorial
Shell tutorialShell tutorial
Shell tutorial
 
Performance Teting - VU Scripting Using Rational (http://www.geektester.blogs...
Performance Teting - VU Scripting Using Rational (http://www.geektester.blogs...Performance Teting - VU Scripting Using Rational (http://www.geektester.blogs...
Performance Teting - VU Scripting Using Rational (http://www.geektester.blogs...
 
LOSS_C11- Programming Linux 20221006.pdf
LOSS_C11- Programming Linux 20221006.pdfLOSS_C11- Programming Linux 20221006.pdf
LOSS_C11- Programming Linux 20221006.pdf
 
POS 433 Effective Communication - tutorialrank.com
POS 433 Effective Communication - tutorialrank.comPOS 433 Effective Communication - tutorialrank.com
POS 433 Effective Communication - tutorialrank.com
 
Tool Development 08 - Windows Command Prompt
Tool Development 08 - Windows Command PromptTool Development 08 - Windows Command Prompt
Tool Development 08 - Windows Command Prompt
 
Ch01
Ch01Ch01
Ch01
 
Shell-Scripting-1.pdf
Shell-Scripting-1.pdfShell-Scripting-1.pdf
Shell-Scripting-1.pdf
 
Shellscripting
ShellscriptingShellscripting
Shellscripting
 
BASIC COMMANDS OF LINUX
BASIC COMMANDS OF LINUXBASIC COMMANDS OF LINUX
BASIC COMMANDS OF LINUX
 
101 3.1 gnu and unix commands
101 3.1 gnu and unix commands101 3.1 gnu and unix commands
101 3.1 gnu and unix commands
 
PowerShell Workshop Series: Session 2
PowerShell Workshop Series: Session 2PowerShell Workshop Series: Session 2
PowerShell Workshop Series: Session 2
 
Linux Basics.pptx
Linux Basics.pptxLinux Basics.pptx
Linux Basics.pptx
 

Linux week7

  • 1. Week 7 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 Linux+ Guide to Linux Certification, 2e 2
  • 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 Linux+ Guide to Linux Certification, 2e 3
  • 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 with command  File descriptors: Numeric labels that define command input and command output Linux+ Guide to Linux Certification, 2e 4
  • 5. 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
  • 6. Command Input and Output (continued) Figure 7-1: The three common file descriptors Linux+ Guide to Linux Certification, 2e 6
  • 7. Redirection  Redirect stdout and stderr from terminal screen to a file Use “>” shell metacharacter Can redirect stdout and stderr to separate files  Use separate filenames for stdout and stderr Linux+ Guide to Linux Certification, 2e 7
  • 8. 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
  • 9. Redirection (continued) Table 7-1: Common redirection examples Linux+ Guide to Linux Certification, 2e 9
  • 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 Linux+ Guide to Linux Certification, 2e 10
  • 11. Pipes (continued) Figure 7-2: Piping information from one command to another Linux+ Guide to Linux Certification, 2e 11
  • 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 commands: Filter commands that also send information to a file Linux+ Guide to Linux Certification, 2e 12
  • 13. Pipes (continued) Figure 7-3: Piping several commands Linux+ Guide to Linux Certification, 2e 13
  • 14. Pipes (continued) Linux+ Guide to Linux Certification, 2e 14
  • 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 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
  • 16. 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
  • 17. Environment Variables  set command: Lists environment variables and current values  echo command: View contents a specified variable Use $ shell metacharacter  Changing value of a variable: Specify variable name followed by equal sign (=) and new value Linux+ Guide to Linux Certification, 2e 17
  • 18. Environment Variables (continued) Table 8-3: Common BASH environment variables Linux+ Guide to Linux Certification, 2e 18
  • 19. Environment Variables (continued) Table 7-3 (continued): Common BASH environment variables Linux+ Guide to Linux Certification, 2e 19
  • 20. Environment Variables (continued) Table 7-3 (continued): Common BASH environment variables Linux+ Guide to Linux Certification, 2e 20
  • 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 Linux+ Guide to Linux Certification, 2e 21
  • 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 Linux+ Guide to Linux Certification, 2e 22
  • 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 ○ Use ; metacharacter Linux+ Guide to Linux Certification, 2e 23
  • 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 Linux+ Guide to Linux Certification, 2e 24
  • 25. 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
  • 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 Linux+ Guide to Linux Certification, 2e 26
  • 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 Defines which shell is used to interpret shell script commands Linux+ Guide to Linux Certification, 2e 27
  • 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/write permission: Executed like any executable program Linux+ Guide to Linux Certification, 2e 28
  • 29. Escape Sequences  Character sequences having special meaning in the echo command Prefixed by character Must use –e option in echo command Linux+ Guide to Linux Certification, 2e 29
  • 30. Escape Sequences (continued) Table 7-4: Common echo escape sequences Linux+ Guide to Linux Certification, 2e 30
  • 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 Places in a variable specified by an argument to read command Linux+ Guide to Linux Certification, 2e 31
  • 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 Linux+ Guide to Linux Certification, 2e 32
  • 33. Decision Constructs (continued) Figure 7-4: A sample decision construct Linux+ Guide to Linux Certification, 2e 33
  • 34. Decision Constructs (continued) Figure 7-5: A sample decision construct Linux+ Guide to Linux Certification, 2e 34
  • 35. The if Construct  Control flow of program based on true/false decisions  Syntax: Linux+ Guide to Linux Certification, 2e 35
  • 36. The if Construct (continued)  Common rules governing if constructs: elif (else if) and else statements optional Unlimited number of elif statements do 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
  • 37. The if Construct (continued)  test statement: Used to test a condition Generates a true/false value Inside of square brackets ( [ … ] ) ○ Must have spaces after “[” and before “]”  Special comparison operators: –o (OR) –a (AND) ! (NOT) Linux+ Guide to Linux Certification, 2e 37
  • 38. The if Construct (continued) Table 7-5: Common test statements Linux+ Guide to Linux Certification, 2e 38
  • 39. The if Construct (continued) Table 7-6: Special operators in test statements Linux+ Guide to Linux Certification, 2e 39
  • 40. The case Construct  Compares value of a variable with several different patterns of text or numbers  Syntax: Linux+ Guide to Linux Certification, 2e 40
  • 41. 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
  • 42. The && and || Constructs  Time-saving shortcut constructs When only one decision needs to be made during execution  Syntax: command && command command || command Linux+ Guide to Linux Certification, 2e 42
  • 43. 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
  • 44. 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
  • 45. 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
  • 46. 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