Skip to content

Linting Shell Scripts

Overview

Shell script are very important in the middle of infrastructure, which they can:

  • Create/Update/Delete resources based on the user permissions

  • Make the procedure of work based on API calls to various of the system

So linting script is the last progress to make script more maintain, easy to understanding and update.

The scirpt is writing when I has a change to review the scirpt at the organization level.

Methodology

Google Cloud Platform

For the Google Cloud Platform, we usually used the gcloud CLI to working with the components in our Cloud project.

Always make sure

Alway remember to declare region and project ID as much as you can.

Comment should short and not to duplicate to the serivces

The comment line is duplicate to the functions: gcloud workflows delete. So we just need delete to mark down the process

Example:

# Delete workflow Listen Tunnel
gcloud workflows delete $WORKFLOW_NAME \
    --location $PROJECT_REGION \
    --project $PROJECT_ID \
    --quiet;
# Delete
gcloud workflows delete $WORKFLOW_NAME \
    --location $PROJECT_REGION \
    --project $PROJECT_ID \
    --quiet;

Shell script are easy to invoke by user with command line like bash <shell-script-path>. This make they will call the process harm to the services.

Instead of that, we will comment to prevent this mistake

# Delete
gcloud workflows delete $WORKFLOW_NAME \
    --location $PROJECT_REGION \
    --project $PROJECT_ID \
    --quiet;
# Delete
# gcloud workflows delete $WORKFLOW_NAME \
#     --location $PROJECT_REGION \
#     --project $PROJECT_ID \
#     --quiet;

Write the comment in a effecient way

a)

#Verify

The space of between text

b) Add note that has more info for the user/maintainer

E.g: The element should be included the comment

  • The URL reference link has the document for usage the service or task

  • The StackoverFlow that ref to the answer why we use that task

  • The elem

# Troubleshooting
# Pause
# Ref: https://cloud.google.com/sdk/gcloud/reference/tasks/queues/pause
# gcloud tasks queues pause $TASK_QUEUE_NAME --location=$PROJECT_REGION;

# Resume
# Ref: https://cloud.google.com/sdk/gcloud/reference/tasks/queues/resume
# gcloud tasks queues resume $TASK_QUEUE_NAME --location=$PROJECT_REGION;

# Ref: https://cloud.google.com/sdk/gcloud/reference/tasks/queues/purge
# gcloud tasks queues purge $TASK_QUEUE_NAME --location=$PROJECT_REGION --quiet;

Check the requirement of inputs are satisfied to the services

Must check the source components has all the required you need

source .../script/config.sh
# Create Queue
# Ref: https://cloud.google.com/sdk/gcloud/reference/tasks/queues/create
gcloud tasks queues create $TASK_QUEUE_NAME --location=$PROJECT_REGION --max-attempts=3 \
    --max-retry-duration=60s --max-doublings=2 --min-backoff=1s \
    --max-backoff=10s --max-dispatches-per-second=10 \
    --max-concurrent-dispatches=10;
# Create Queue
# Ref: https://cloud.google.com/sdk/gcloud/reference/tasks/queues/create
gcloud tasks queues create $TASK_QUEUE_NAME \
    --location=$PROJECT_REGION \
    --max-attempts=3 \
    --max-retry-duration=60s \
    --max-doublings=2 \
    --min-backoff=1s \
    --max-backoff=10s \
    --max-dispatches-per-second=10 \
    --max-concurrent-dispatches=10;

Alway append ; in the end of the statement if you can.

This prevent to not append other statements into the component at the runtime. This usually failed but it create a not detect type of errors.

Group components

Reference