Redirecting Input and Output to Files

Tutorial 3 of 5

Introduction

  • Goal: The aim of this tutorial is to equip you with the knowledge of input and output redirection in shell scripts. This includes learning how to direct the flow of data, which allows programs to take input from files and send output to other files.
  • Learning Outcomes: You will learn the syntax and usage of shell redirection operators, and how to use them in your shell scripts. You will also learn how to handle errors using redirection.
  • Prerequisites: This tutorial assumes that you have a basic understanding of shell scripts and are familiar with command line interfaces.

Step-by-Step Guide

Input Redirection

The < character is used for input redirection. It instructs the shell to read input from a file.

Example: command < file

This tells the shell to execute command on file.

Output Redirection

The > character is used for output redirection. It instructs the shell to send the output of a command to a file.

Example: command > file

This tells the shell to execute command and then write the output to file.

Error Redirection

The 2> character is used for error redirection. It instructs the shell to send the error output of a command to a file.

Example: command 2> file

This tells the shell to execute command and then write any error messages to file.

Append Operator

The >> operator is used to append the output of a command to a file instead of overwriting the file.

Example: command >> file

This tells the shell to execute command and then append the output to file.

Code Examples

# Input Redirection
sort < file.txt
# This command sorts the contents of file.txt.

# Output Redirection
ls -l > file.txt
# This command writes the long listing of the current directory to file.txt.

# Error Redirection
ls -l non_existent_directory 2> error.txt
# This command tries to list a non-existent directory and writes the error message to error.txt.

# Append Operator
echo "Hello, World!" >> greetings.txt
# This command appends "Hello, World!" to greetings.txt.

Summary

  • We learned about input and output redirection in shell scripts.
  • We learned how to use the <, >, 2>, and >> operators for redirection.
  • We saw examples of how to use these operators in shell scripts.

Next Steps

  • Practice writing shell scripts using the redirection operators.
  • Learn about pipes (|), which allow for chaining commands and redirecting output from one command as input to another.
  • Learn about file descriptors and how they're used in redirection.

Additional Resources

Practice Exercises

  1. Write a script that sorts the contents of a file and writes the sorted content to a new file.
  2. Write a script that counts the number of lines in a file and appends the count to another file.
  3. Write a script that tries to access a non-existent directory and writes the error message to a file.

Solutions

# Exercise 1
sort < file.txt > sorted_file.txt

# Exercise 2
wc -l < file.txt >> line_count.txt

# Exercise 3
ls -l non_existent_directory 2> error.txt