0
0
Bash Scriptingscripting~15 mins

File descriptors and redirection in Bash Scripting - Mini Project: Build & Apply

Choose your learning style9 modes available
File Descriptors and Redirection in Bash
📖 Scenario: You are working on a small bash script to manage output messages. You want to separate normal messages from error messages by using file descriptors and redirection.
🎯 Goal: Build a bash script that writes a normal message to standard output and an error message to standard error using file descriptors and redirection.
📋 What You'll Learn
Create a file descriptor for error messages
Redirect error messages to a separate file
Write a normal message to standard output
Write an error message to the error file using the file descriptor
Display the contents of both output and error files
💡 Why This Matters
🌍 Real World
Separating normal output and error messages helps in debugging scripts and logging important information separately.
💼 Career
Understanding file descriptors and redirection is essential for writing robust shell scripts used in system administration and automation tasks.
Progress0 / 4 steps
1
Create output and error files
Create two empty files called output.txt and error.txt using the touch command.
Bash Scripting
Need a hint?

Use the touch command to create empty files.

2
Redirect file descriptor 3 to error.txt
Add a command to redirect file descriptor 3 to write to the file error.txt.
Bash Scripting
Need a hint?

Use exec 3>error.txt to open file descriptor 3 for writing to error.txt.

3
Write messages using redirection
Write the message "This is a normal message." to output.txt using standard output redirection, and write the message "This is an error message." to error.txt using file descriptor 3.
Bash Scripting
Need a hint?

Use echo "message" > output.txt for normal output and echo "message" >&3 to write to file descriptor 3.

4
Display contents of output and error files
Print the contents of output.txt and error.txt using the cat command to show the messages stored in each file.
Bash Scripting
Need a hint?

Use cat output.txt and cat error.txt to display the contents of the files.