w3resource

Bash I/O Redirection: Exercises, Solution and Explanation

1.

Write a Bash script that redirects the output of the ls command to a file named "test.txt". Print the content of list.txt.

Code:

#!/bin/bash
# Shebang line: Indicates the path to the shell interpreter (in this case, bash)

# Redirecting the output of the ls command to a file named "list.txt"
ls > test.txt

Explanation:

In the exercise above,

  • The "ls" command lists the contents of the current directory.
  • The > symbol is used for output redirection, which directs the output of the "ls" command to the file named "test.txt".
  • When you run this script, it will execute the "ls" command and save its output to a file named 'test.txt' in the current directory.

2.

Write a Bash script that uses input redirection to read the contents of a file named "exec_stderr.txt" and then echoes those contents to the terminal.

Code:

#!/bin/bash
# Shebang line: Indicates the path to the shell interpreter (in this case, bash)

# Using input redirection to read the contents of "exec_stderr.txt" and echoing them to the terminal
cat < exec_stderr.txt

Explanation:

In the exercise above,

  • The "cat" command is used for concatenating and displaying files. When used without any filenames, it reads from standard input.
  • '<' symbol is used for input redirection, which directs the contents of the file "exec_stderr.txt" to the standard input of the "cat" command.
  • The "cat" command then reads the contents of the 'exec_stderr.txt' file and echoes (displays) them to the terminal.

3.

Write a Bash script that uses both input and output redirection to read the contents of a file named "input.txt" and write them to a new file named "output.txt".

Code:

#!/bin/bash
# Shebang line: Indicates the path to the shell interpreter (in this case, bash)

# Using input redirection to read the contents of "input.txt" and output redirection to write them to "output.txt"
cat < input.txt > output.txt

Explanation:

In the exercise above,

  • The "cat" command is used for concatenating and displaying files. When used without any filenames, it reads from standard input.
  • '<' symbol is used for input redirection, which directs the contents of the file "input.txt" to the standard input of the "cat" command.
  • '>' symbol is used for output redirection, which directs the output of the "cat" command to the file "output.txt".
  • The "cat" command reads the contents of the "input.txt" file and writes them to the 'output.txt' file.

4.

Write a Bash script that redirects the standard error (stderr) of a command to a file named "error.log".

Code:

#!/bin/bash
# Shebang line: Indicates the path to the shell interpreter (in this case, bash)

# Redirecting the standard error (stderr) of a command to a file named "error.log"
command_that_might_generate_errors 2> error.log

Explanation:

In the exercise above,

  • "command_that_might_generate_errors" represents the command that might generate errors.
  • '2>' symbol is used for stderr redirection, which redirects the standard error (stderr) output of the command to the file "error.log".

5.

Write a Bash script that appends the output of the date command to a file named "log.txt" without overwriting its existing contents.

Code:

#!/bin/bash
# Shebang line: Indicates the path to the shell interpreter (in this case, bash)

# Appending the output of the date command to "log.txt" without overwriting its existing contents
date >> log.txt

Explanation:

In the exercise above,

  • The "date" command is used to display the current date and time.
  • ">>" symbol is used for output redirection, which appends the output of the "date" command to the file "log.txt" without overwriting its existing contents.

6.

Write a Bash script that redirects the output of the echo command to /dev/null to suppress any output.

Code:

#!/bin/bash
# Shebang line: Indicates the path to the shell interpreter (in this case, bash)

# Redirecting the output of the echo command to /dev/null to suppress any output
echo "This output will be suppressed" > /dev/null

Explanation:

In the exercise above,

  • The "echo" command prints the string "This output will be suppressed".
  • "> /dev/null" redirects the output of the "echo" command to "/dev/null", which is a special file that discards all data written to it. Therefore, any output generated by the "echo" command will be suppressed and not displayed in the terminal.

7.

Write a Bash script that redirects the output of a command to another command as input, such as ls | grep .txt to list only files with a ".txt" extension.

Code:

#!/bin/bash
# Shebang line: Indicates the path to the shell interpreter (in this case, bash)

# Redirecting the output of the ls command to grep as input, to list only files with a ".txt" extension
ls | grep '\.txt$'

Output:

file1.txt
file2.txt
input.txt
output.txt
sample.txt

Explanation:

In the exercise above,

  • The "ls" command lists the contents of the current directory.
  • The '|' symbol (pipe) is used to redirect the output of the "ls" command as input to the grep command.
  • grep '\.txt$' filters the input from "ls", listing only lines that end with the ".txt" extension. \ is used to escape the dot . character in the regular expression pattern to match the literal dot character, and '$' represents the end of the line.

8.

Write a Bash script that uses input redirection to read a number from a file named "nums.txt" and then performs some arithmetic operation on it.

Code:

#!/bin/bash
# Shebang line: Indicates the path to the shell interpreter (in this case, bash)

# Using input redirection to read a number from "nums.txt"
# Performing arithmetic operation on the read number
read number < nums.txt
result=$((number * 2))

# Printing the result of the arithmetic operation
echo "The result of doubling the number from nums.txt is: $result"

Output:

The result of doubling the number from nums.txt is: 10

Explanation:

In the exercise above,

  • read number < nums.txt reads the number from the file "nums.txt" and stores it in the variable number.
  • result=$((number * 2)) performs the arithmetic operation of doubling the number stored in 'number' and assigns the result to the variable 'result'.
  • Finally, the script prints the result of doubling the number from "nums.txt" to the terminal.

9.

Write a Bash script that redirects the output of a command to both the terminal and a file simultaneously, using the tee command.

Code:

#!/bin/bash
# Shebang line: Indicates the path to the shell interpreter (in this case, bash)

# Redirecting the output of the command to both the terminal and a file using tee
ls -l | tee output.txt

Output:

total 0
-rw-r--r-- 1 rg rg  67 Apr 13 18:11 error.log
-rw-r--r-- 1 rg rg 284 Apr 13 17:41 file1.txt
-rw-r--r-- 1 rg rg 284 Apr 13 17:41 file2.txt
-rw-r--r-- 1 rg rg 284 Apr 13 18:09 input.txt
-rw-r--r-- 1 rg rg   2 Apr 13 18:15 nums.txt
-rw-r--r-- 1 rg rg 284 Apr 13 18:10 output.txt
-rw-r--r-- 1 rg rg 313 Apr 13 18:13 sample.txt
drwxrwxrwx 1 rg rg 512 Jun 10  2022 test
-rwxr-xr-x 1 rg rg 200 Apr 13 18:17 test.sh

Explanation:

In the exercise above,

  • The ls -l command lists the contents of the current directory in long format.
  • The | symbol (pipe) is used to redirect the output of the ls -l command as input to the tee command.
  • tee output.txt redirects the output to both the terminal and a file named "output.txt". The "tee" command reads from standard input and writes to both standard output (the terminal) and the specified file.

10.

Write a Bash script that uses a heredoc to input multi-line text and redirects it to a file named "document.txt".

Code:

#!/bin/bash
# Shebang line: Indicates the path to the shell interpreter (in this case, bash)

# Using a heredoc to input multi-line text and redirecting it to a file named "document.txt"
cat < document.txt
This is line 1 of the document.
This is line 2 of the document.
This is line 3 of the document.
EOF

Explanation:

In the exercise above,

  • <<EOF starts a heredoc block, which allows us to input multi-line text.
  • The text within the heredoc block is then written to the file "document.txt".
  • EOF marks the end of the heredoc block.
  • When you run this script, it will create a file named "document.txt" with the specified multi-line text. Each heredoc block line will be written to the file "document.txt".

Bash Editor:


More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.



Follow us on Facebook and Twitter for latest update.