Create Shell Scripts to Process Command Output

Red Hat Enterprise LinuxBeginner
Practice Now

Introduction

In this challenge, you will learn how to create a simple shell script that processes the output of shell commands. This is a fundamental skill for automating tasks and extracting specific information from command-line tools. You will learn how to capture command output, manipulate the data, and use the processed information within your script. This challenge will help you develop practical scripting skills essential for system administration.

Create a Shell Script to Process Command Output

In this step, you will create a shell script that captures the output of the ls -l command, extracts specific information (file size and file name), and displays it in a formatted way. This exercise demonstrates how to parse command output, which is a common task in shell scripting.

Tasks

  • Create a shell script that captures the output of the ls -l command.
  • Extract the file size and file name from the ls -l output for each file.
  • Display the extracted information in a specific formatted output.

Requirements

  • Create the script in the ~/project/scripts directory.
  • Name the script process_ls.sh.
  • The script must start with the #!/bin/bash shebang.
  • The script should capture the output of the ls -l command and store it in a variable.
  • For each file listed in the ls -l output, extract its size and name.
  • Display the extracted file size and file name in the following format:
    File size: <file_size> bytes
    File name: <file_name>
    Replace <file_size> with the actual file size in bytes and <file_name> with the actual file name.

Example

Let's assume you have a directory with files. When you run your script, the output should look similar to this:

[labex@host ~]$ cd ~/project/scripts
[labex@host scripts]$ nano process_ls.sh
[labex@host scripts]$ chmod +x process_ls.sh
[labex@host scripts]$ ./process_ls.sh
File size: 1300 bytes
File name: process_ls.sh
[labex@host scripts]$

The exact file sizes and names will depend on the contents of your current directory.

Hints

  • You can use command substitution (e.g., variable=$(command)) to capture the output of a command.
  • To process each line of the command output, you can use a while read loop.
  • The ls -l command provides detailed information. You might need to use tools like awk or cut to extract specific columns (fields) from each line of its output.
  • Remember that the first line of ls -l output often starts with "total" and should be skipped when processing individual files.
  • Ensure your script has execute permissions (chmod +x).
✨ Check Solution and Practice

Summary

In this challenge, you learned how to create a shell script to process the output of a command. Specifically, you captured the output of ls -l, extracted file sizes and names, and displayed them in a formatted way. This exercise demonstrated key shell scripting concepts such as command substitution, while read loops, and text processing with awk. These skills are crucial for automating tasks, parsing log files, and extracting specific data from various command-line tools, which are common requirements for the RHCSA exam and daily system administration.