Data Streams: Standard Input, Output, and Error

Data Streams: Standard Input, Output, and Error

Introduction

  • Data streams control the flow of output and errors and enable user input.
  • Concepts are crucial Linux-wide, not just for Bash scripting.
  • Three types of data streams: standard input, standard output, and standard error.

Standard Output

  • Standard output is the default destination for command results.
  • It is output printed on the screen that is not an error.
  • Example: The ls command.

Standard Error

  • Standard error is output printed to the screen that signifies an error.
  • Example: listing the contents of a non-existent directory.
  • Distinguishing between standard output and standard error is important.
  • Use echo $? to check the exit code of the previous command (non-zero indicates failure).

Example: Standard Output vs. Standard Error

  • Successful command (echo hello world) returns an exit code of 0; "hello world" is standard output.
  • Failed command (ls on a non-existent directory) returns a non-zero exit code; error message is standard error.

The find Command

  • The find command locates files and directories based on search criteria.
  • Example: find /etc -type f lists all files in the /etc directory.
  • This command can produce both standard output (valid results) and standard error (permission denied).

Redirecting Standard Error

  • Use 2> to redirect standard error.
  • /dev/null is a special location that deletes any data sent to it.
  • Example: find /etc -type f 2> /dev/null sends standard error to /dev/null, hiding errors.
  • The command still executes the same way and produces the same errors. We are just choosing not to see them.

Redirecting Standard Output

  • Without specifying 1, redirection defaults to standard output.
  • Example: find /etc -type f > /dev/null sends standard output to /dev/null, displaying only errors.
  • 1 designates standard output, and 2 designates standard error.
  • 1> is equivalent to > because standard output is implied.
  • Redirecting to a file overwrites the file's contents unless appending (>>) is used.

Usefulness in Scripts

  • Differentiate between standard output and standard error for logging purposes.
  • Send standard error to a separate log file or email for error tracking.
  • Example Scenario:
    • Standard Output: Log file
    • Standard Error: Different log file; email notification

Redirecting Both Standard Output and Standard Error

  • Use &> to redirect both standard output and standard error to the same location.
  • Example: find /etc -type f &> file.txt sends both streams to file.txt, overwriting the file.

Redirecting to Different Locations

  • Example: find /etc -type f 1> find_results.txt 2> find_errors.txtseparates standard output and standard error into different files.
  • Error messages are copied into an error log, and successful messages are copied into a success log.

Script Example: Universal Updater

  • Modifying the Universal Updater script to utilize data streams.
  • New variables: log_file and error_log to specify log file paths under /var/log.
  • Checking exit codes to determine if an error has occurred.
  • Redirect output using 1> and 2>>:
    • 1>: Standard output
    • 2>>: Appends standard error
  • Example Implementation:
    • Output: \
      bash sudo pacman -Syu 1>> "$log_file" 2>> "$error_log"
    • If the exit code is anything but 0, an error will be printed.
  • -y option added to sudo apt dist-upgrade to automatically answer "yes" to prompts.
  • Example using the tail command:
    • tail -f /var/log/updater.log watches a text file in real time.

Standard Input

  • The third type of data stream: standard input.
  • Accepting information from a user.
  • Allows the user to input information.
  • Example:
    • Prompt the user to enter their name.
    • Read the input using the read command.
    • Store the input in a variable, like so: read my_name
    • Print what they entered: echo "Your name is $my_name"

Conclusion

  • Data streams give you control over output, allow you to see errors, and enable user inputs in Linux systems.
  • Make sure to retain working knowledge of data-streams in order to use them later.