This post is going to be quick and concise. The main purpose of this script is to define required and optional named arguments in bash. This script is intended to be used as a parser of arguments and showing example usage. Later I will show you how can you include it in your own bash scripts.

Problem: Lets say we have a script that tries to deploy some cloud resources and parse some settings for those resources, where we have a default behavior for most resources except for one.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/bin/bash

# Function to display usage instructions
display_usage() {
    echo "Usage: $0 [options]"
    echo "Options:"
    echo "  --resource1  <value>     Resource name value (required)"
    echo "  --resource2  <value>     Resource name value (required)"
    echo "  --log_mode   <value>     Setting value (optional, default=debug)"
    echo "  --output     <value>     Setting value (optional, default=s3)"
    echo "  --format     <value>     Setting value (optional, default=json)"
    exit 1
}

# Function to parse command line arguments
parse_arguments() {
    while [[ $# -gt 0 ]]; do
        key="$1"
        case $key in
            --resource1)
                resource1="$2"
                shift
                ;;
            --resource2)
                resource2="$2"
                shift
                ;;
            --log_mode)
                log_mode="$2"
                shift
                ;;
            --output)
                output="$2"
                shift
                ;;
            --format)
                format="$2"
                shift
                ;;
            *)
                # Unknown option
                echo "Unknown option: $key"
                display_usage
                ;;
        esac
        shift
    done

    # Check for required parameters
    if [[ -z $resource1 ]]; then
        echo "Missing required parameter: --resource1 (example: db_instance)"
        display_usage
    fi

    if [[ -z $resource2 ]]; then
        echo "Missing required parameter: --resource2 (example: db_instance)"
        display_usage
    fi


    # Set default values for optional parameters
    if [[ -z $log_mode ]]; then
        # optional
        log_mode=debug
    fi

    if [[ -z $output ]]; then
        # optional
        output=s3
    fi

    if [[ -z $format ]]; then
        # optional
        format=json
    fi
}

# Call the argument parsing function
parse_arguments "$@"

# Display the parsed values
echo "Resource 1: $resource1"
echo "Resource 2: $resource2"
echo "Settings: $log_mode, $output, $format"

We can save this script with the name parser_helper.sh and we can use it in another script as such:

1
2
3
4
5
6
7
#!/bin/bash

# include parser
source parser_helper.sh

# Commands in logic.sh
...