How to accept command-line arguments in a shell script?

Accepting Command-Line Arguments in a Shell Script

In shell scripting, command-line arguments are a powerful way to pass dynamic data to your script, allowing it to be more flexible and reusable. By accepting command-line arguments, you can make your script more versatile and enable it to handle different scenarios without the need to modify the script itself.

Understanding Command-Line Arguments

When you run a shell script, you can pass one or more arguments to it. These arguments are stored in special variables that you can access within the script. The most commonly used variables for command-line arguments are:

  1. $0: The name of the script itself.
  2. $1, $2, $3, ..., $9: The first, second, third, ..., ninth command-line arguments, respectively.
  3. $#: The number of command-line arguments passed to the script.
  4. $@ or $*: All the command-line arguments as a single string.

Here's an example to illustrate how these variables work:

#!/bin/bash

echo "Script name: $0"
echo "First argument: $1"
echo "Second argument: $2"
echo "Number of arguments: $#"
echo "All arguments: $@"

If you save this script as example.sh and run it with the following command:

./example.sh hello world

The output will be:

Script name: ./example.sh
First argument: hello
Second argument: world
Number of arguments: 2
All arguments: hello world

Handling Command-Line Arguments

To handle command-line arguments in your shell script, you can use a combination of these variables. Here are a few common use cases:

  1. Accessing individual arguments:

    echo "First argument: $1"
    echo "Second argument: $2"
  2. Checking the number of arguments:

    if [ $# -ne 2 ]; then
      echo "Usage: $0 <arg1> <arg2>"
      exit 1
    fi
  3. Looping through all arguments:

    for arg in "$@"; do
      echo "Argument: $arg"
    done
  4. Parsing options and flags:

    while [[ $# -gt 0 ]]; do
      key="$1"
      case $key in
        -f|--file)
          FILE_PATH="$2"
          shift # past argument
          shift # past value
          ;;
        -d|--directory)
          DIR_PATH="$2"
          shift # past argument
          shift # past value
          ;;
        *)
          # unknown option
          ;;
      esac
    done

This example demonstrates how to parse options and flags passed to the script, allowing the user to specify a file path and a directory path.

Visualizing the Concept

Here's a Mermaid diagram that illustrates the concept of command-line arguments in a shell script:

graph TD A[Run Shell Script] --> B[Script Execution] B --> C[Access Command-Line Arguments] C --> D[Special Variables] D --> E[$0: Script Name] D --> F[$1, $2, ..., $9: Arguments] D --> G[$#: Number of Arguments] D --> H[$@: All Arguments] C --> I[Handle Arguments] I --> J[Access Individual Arguments] I --> K[Check Number of Arguments] I --> L[Loop Through Arguments] I --> M[Parse Options and Flags]

By understanding how to accept and handle command-line arguments, you can create more versatile and dynamic shell scripts that can adapt to different user needs and scenarios.

0 Comments

no data
Be the first to share your comment!