Skip to content

Boaxyl/working-with-function

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 

Repository files navigation

working-with-function

Step-by-step explanation

Invocation and first variable

ENVIRONMENT=$1 stores the first CLI argument in ENVIRONMENT.

The script expects one argument that indicates the target environment (local, testing, production).

check_num_of_args() function

Purpose: ensure exactly one argument was passed.

Key line: if [ "$#" -ne 1 ]; then ... exit 1; fi

Note: The function is invoked as check_num_of_args "$@" so that inside the function "$#" reflects the actual number of CLI args.

Effect: If the number of args is not 1, the script prints usage and exits with code 1.

activate_infra_environment() function

Purpose: act based on the value of ENVIRONMENT.

Logic:

If ENVIRONMENT equals local, print local message.

If testing, print testing message.

If production, print production message.

Otherwise print an error and exit with code 2.

Notes:

Uses == inside [ ... ] which is supported by bash. For strict POSIX compatibility use = instead.

Exiting with code 2 signals an invalid environment argument.

check_aws_cli() function

Purpose: verify AWS CLI is available on PATH.

Key line: if ! command -v aws &> /dev/null; then ... return 1; fi

Behavior: If aws is not found, the function prints a message and returns 1.

Important: return 1 inside a function does not by itself stop the script. The caller must check the return code or the script must use set -e to fail fast.

check_aws_profile() function

Purpose: ensure AWS_PROFILE environment variable is set.

Key line: if [ -z "$AWS_PROFILE" ]; then ... return 1; fi

Behavior: If AWS_PROFILE is empty or unset, it prints a message and returns 1.

Same note as above about return vs exit.

Execution order

check_num_of_args "$@" runs first. If it exits, nothing else runs.

activate_infra_environment runs next and may exit on invalid environment.

check_aws_cli runs next. It returns non-zero on failure but the script will continue unless you change behavior.

check_aws_profile runs last. It returns non-zero on failure but the script will continue unless you change behavior.

Example usage and expected outputs

Correct call:

./script.sh local

Output: Running script for Local Environment...

Missing argument:

./script.sh

Output: Usage: ./script.sh and script exits with code 1.

Invalid environment:

./script.sh staging

Output: Invalid environment specified. Please use 'local', 'testing', or 'production'. and exits with code 2.

AWS CLI not installed:

Script prints AWS CLI is not installed. Please install it before proceeding. but will continue unless you change return handling.

AWS_PROFILE not set:

Script prints AWS profile environment variable is not set. but will continue unless you change return handling.

Troubleshooting tips and common fixes

If you want the script to stop when check_aws_cli or check_aws_profile fail:

Replace return 1 with exit 1, or after the call check the return code:

check_aws_cli || exit 1 check_aws_profile || exit 1

Or enable fail-fast at the top: set -euo pipefail

If you want a default environment instead of forcing the user to pass one:

Example: ENVIRONMENT=${1:-local} will default to local when no arg provided. Adjust check_num_of_args accordingly.

To set AWS profile quickly:

export AWS_PROFILE=my-profile

Or run one-off: AWS_PROFILE=my-profile ./script.sh production

Small improvements you may want to add

Fail fast and safe mode:

#!/bin/bash set -euo pipefail IFS=$'\n\t'

ENVIRONMENT=${1:-local} # default to local

... functions ...

check_num_of_args "$@" activate_infra_environment check_aws_cli || { echo "Please install aws cli"; exit 1; } check_aws_profile || { echo "Please set AWS_PROFILE"; exit 1; }

Use = instead of == for POSIX compatibility:

if [ "$ENVIRONMENT" = "local" ]; then

Quick summary

The script validates that one argument is provided, decides which environment to operate for, and checks for AWS CLI and an AWS profile.

exit stops the script immediately. return only exits a function. Use exit or check return codes when you want the whole script to stop on failure.

Consider set -euo pipefail for safer behavior and simpler error handling

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published