Docsity

BASH Shell: Input and Output Redirection, Environment Variables, and Shell Scripts, Slides of Advanced Operating Systems

Partial preview of the text

Download BASH Shell: Input and Output Redirection, Environment Variables, and Shell Scripts and more Slides Advanced Operating Systems in PDF only on Docsity!

Chapter 8

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

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

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

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
  • Otherwise, use the &
  • Ex: .$ls โ€“ l /etc/host /etc/h >jj 2>&

Redirection (continued)

  • Redirecting stdin to a file:
    • Use โ€œ<โ€œ shell metacharacter
  • tr command: Replace characters in a file sent via stdin
  • tr accepts files not file name so you need to use input redirection
  • The shell always clears the content of the file specified in the output redirection

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 (using grep)

Pipes (continued)

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

Pipes (continued)

Figure 8-3: Piping several commands

Pipes (continued)

Table 8-2: Common filter commands

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

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

Environment Variables (continued)

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

Environment Variables (continued)

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