Create Shell Scripts to Process Command Output

LinuxLinuxBeginner
Practice Now

Introduction

In this challenge, you will learn how to create a simple shell script that processes the output of shell commands. You will learn how to capture the output of commands, manipulate the data, and use the processed information within the script. This challenge will help you develop the skills required to automate tasks and create more complex shell scripts.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") subgraph Lab Skills linux/echo -.-> lab-389451{{"`Create Shell Scripts to Process Command Output`"}} end

Create a Shell Script to Process Command Output

Tasks

  • Create a shell script that captures the output of the ls -l command and processes the information.
  • Extract the file size and file name from the output and display them in a formatted way.

Requirements

  • Create the script in the ~/project/scripts directory and name it process_ls.sh.
  • Use the bash shebang at the beginning of the script.
  • The script should capture the output of the ls -l command and store it in a variable.
  • Extract the file size and file name from the output and display them in the following format:
    File size: <file_size> bytes
    File name: <file_name>
  • Repeat this for each file listed in the ls -l output.

Example

File size: 4096 bytes
File name: file1.txt
File size: 8192 bytes
File name: file2.txt

Summary

In this challenge, you learned how to create a simple shell script that processes the output of the ls -l command. You learned how to capture the output of a command, extract specific information from it, and display the processed data in a formatted way. This challenge helps you develop the skills required to automate tasks and create more complex shell scripts.

Other Linux Tutorials you may like