Process Arguments in Bash Scripts

LinuxLinuxBeginner
Practice Now

Introduction

In this challenge, you will create a bash script that processes command-line arguments. This script will demonstrate key concepts in bash scripting, including handling command-line arguments, using special variables, and displaying process information. This task is essential for system administrators and developers who need to create flexible and informative command-line tools.

Environment

LabEx uses Red Hat Universal Base Image 9 (UBI9) to simulate the exam environment. It may not be identical to the actual RHCSA exam environment, but it provides a good representation of the tasks you'll encounter.

There are two users in the environment:

  • labex: A standard user with sudo privileges, password: labex.
  • root: The system administrator, password: redhat.

The challenge features real exam questions, along with explanations, requirements, and automated verification scripts to help you confirm task completion. It effectively simulates the knowledge areas covered in the RHCSA exam.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL shell(("Shell")) -.-> shell/VariableHandlingGroup(["Variable Handling"]) linux(("Linux")) -.-> linux/BasicFileOperationsGroup(["Basic File Operations"]) shell(("Shell")) -.-> shell/FunctionsandScopeGroup(["Functions and Scope"]) shell/VariableHandlingGroup -.-> shell/variables_usage("Variable Usage") shell/VariableHandlingGroup -.-> shell/param_expansion("Parameter Expansion") linux/BasicFileOperationsGroup -.-> linux/chmod("Permission Modifying") shell/FunctionsandScopeGroup -.-> shell/func_def("Function Definition") subgraph Lab Skills shell/variables_usage -.-> lab-416107{{"Process Arguments in Bash Scripts"}} shell/param_expansion -.-> lab-416107{{"Process Arguments in Bash Scripts"}} linux/chmod -.-> lab-416107{{"Process Arguments in Bash Scripts"}} shell/func_def -.-> lab-416107{{"Process Arguments in Bash Scripts"}} end

Create and Configure the Bash Script

Let's create a bash script that processes and displays information about the arguments passed to it.

Tasks

  • Create a bash script named argts.sh in the /home/labex/ directory
  • Make the script executable
  • Implement the script to display the following information:
    • Total count of supplied arguments
    • Value of the first argument
    • PID of the script
    • All supplied arguments

Requirements

  • The script must be created at /home/labex/argts.sh
  • The script must have the correct shebang line: #!/bin/bash
  • The script must be executable
  • The script must use appropriate bash special variables to display the required information
  • The output should be formatted clearly with a header line of equal signs

Example

When the script is run with arguments, it should produce output similar to this:

$ /home/labex/argts.sh one two three
===========================
The total count of the supp argts: 3
The value of the first arg is: one
The PID of the script is : 12345
All the argts: one two three
โœจ Check Solution and Practice

Summary

In this challenge, you created a bash script that processes and displays information about command-line arguments. This exercise demonstrates essential bash scripting concepts such as accessing special variables ($#, $1, $$, $*), creating executable scripts, and formatting output. These skills are crucial for system administrators and developers who need to create flexible command-line tools and process user input effectively.