Skip to content

Working with arrays and functions in shell scripting is essential for writing efficient and reusable code. Arrays allow you to store and manage multiple values under a single variable, while functions let you organize tasks into reusable blocks. Together, they make scripts easier to maintain, scalable, and powerful for automation.

Notifications You must be signed in to change notification settings

ICO-Tech/Working-With-Functions-and-Array-In-Shell-SCripting

Repository files navigation

Working-With-Functions-and-Array-In-Shell-SCripting

Working with arrays and functions in shell scripting is essential for writing efficient and reusable code. Arrays allow you to store and manage multiple values under a single variable, while functions let you organize tasks into reusable blocks. Together, they make scripts easier to maintain, scalable, and powerful for automation.

In this project, I developed a shell script for one of DataWise Solution’s prestigious clients. The script is designed to automate key cloud tasks such as setting up EC2 instances and creating S3 buckets, helping to save time and reduce manual effort. However, the main focus of this project is on demonstrating the use of functions within the script, showing how they simplify the code, improve reusability, and make automation more efficient.

FUNCTION

A function in shell scripting is like a small program inside your script. It is a block of code that has a name and performs a specific task. Instead of writing the same code many times, you can put it in a function and call it whenever needed. This makes your script shorter, easier to read, and reusable.

By putting specific logic inside functions, we make our scripts easier to read and manage.

Let’s take the following steps and turn them into functions:

  • Check if the script has an argument.
  • Check if AWS CLI is installed.
  • Check if the environment variable for AWS authentication exists.

Check if the script has an argument

This Bash script wihtout a function and it's designed to run based on an environment argument provided by the user. It first checks if exactly one argument is passed; if not, it shows usage instructions and exits. The argument is stored in a variable called ENVIRONMENT, and the script then uses conditional checks to determine the environment type. If the user specifies local, testing, or production, the script runs the corresponding environment logic. If an invalid option is entered, it displays an error message and exits.

Encamsulating the Script into a function

The first script checks arguments directly in the main body of the script, mixing logic with execution, while this second script improves structure by placing the argument check inside a function. Both perform the same task, but the second is more modular, readable, and easier to maintain or reuse.

Reason for Using Function

When you define a function in a shell script, it won’t run until you call it. To make the function work, you must include a call in the script. The order of code matters—placing the function in the right spot ensures it is available when needed, making your script easier to read, use, and debug.

Running Environment Specific acttion

his script showed the difference between the three scripts all handle environment-based execution, but differ in structure and function use. The first script checks arguments directly in the main body, mixing validation and logic. The second introduces a function for argument checking but doesn’t clearly show it being called. The third defines the function and explicitly calls it before running environment-specific actions, making it fully modular, organized, and easier to read and maintain.

Real-World Example

In this script, I finally created a more updeted version in a real world context. The latest script is fully modular, separating argument validation and environment actions into two distinct functions, making it more readable, reusable, and easier to maintain.

Check if AWS CLI is installed.

Here i also cretaed a script defines a function called check_aws_cli that verifies whether the AWS CLI is installed on the system. It uses the command -v aws check, and if the AWS CLI is missing, it displays an error message asking the user to install it and returns a failure code (1). This ensures the script stops or warns the user before running AWS-related commands.

Summary

From the scripts, the core idea is learning how to organize and automate tasks in Bash using arguments and functions. The first script introduced passing an argument like local, testing, or production to decide which environment the script should run in. Later versions improved by adding functions such as check_num_of_args (to verify arguments), activate_infra_environment (to run logic for the chosen environment), and checks for AWS setup. The final version became more robust by including check_aws_cli (to ensure AWS CLI is installed) and check_aws_profile (to confirm the AWS profile variable is set). Altogether, these scripts demonstrate step-by-step how to move from a simple argument check to a structured, reliable automation script that is easier to maintain, debug, and extend.

About

Working with arrays and functions in shell scripting is essential for writing efficient and reusable code. Arrays allow you to store and manage multiple values under a single variable, while functions let you organize tasks into reusable blocks. Together, they make scripts easier to maintain, scalable, and powerful for automation.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages