Shell Scripting - Part 1
A shell script is a file containing a series of commands that the shell can execute in order. These scripts can perform various tasks, such as setting up the environment, running programs, manipulating files, and performing system maintenance tasks.
Shell scripts are often used to automate repetitive tasks, which can save time and reduce errors that can occur when these tasks are performed manually.
Shell scripts can be written in various shell languages, including bash, sh, ksh, and csh. They can be executed by typing the name of the script file at the command prompt or by scheduling them to run at specific times using a cron job.
Shell Script -1:
Step 1: Created a file using the touch command.
Step 2: Edited that using nano editor.
Step 3: After adding the content to the file, I had to give the execution permissions to the file using chmod command.
Step 4: You don't have to create a file using .sh, you can use any extension but to identify that file as a shell script we often use .sh format.
Step 5: You can also run this script file using bash without giving the necessary permissions.

Shell Script -2: Variables
A variable in a shell script is a symbolic name that represents a value or a piece of information that can be used and manipulated within the script. In shell scripting, variables are defined using the format variable_name=value.
Created a file and added the below content to the file. Declared Variables in this file and used them to print the required output.


Shell Script -3: Real-time Use Case Scripts
I install AWS CLI for the real-time use cases of shell script demonstration.
Step 1: update ubuntu db using apt-get update command.
Step 2: Installed some useful tools using apt-get install.

Step 3: Now, Let's Install AWS CLI, the commands to install CLI can be found in AWS documentation, you need to search the Web as it updates based on the ubuntu release.
https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"unzip awscliv2.zipsudo ./aws/install
After running the above three commands, AWS CLI is installed on the machine.

Step 4: To provide access to the AWS instance, I'm assigning a role to this instance. Use the below navigation steps to provide access.

Select the ADMIN role for this Instance and update the IAM role, Later you can remove it.

Step 5: After grating the access, now let's come to the use case. We have a requirement to pull the vpcs that are created in our AWS account. There are hundreds of AWS CLI commands available to create, modify, access and delete the AWS resources. Use the below documentation
https://awscli.amazonaws.com/v2/documentation/api/latest/index.html
We need only the aws ec2 describe-vpcs command to pull the information and display it however we want. I installed jq tool which parses the JSON file and shows us in a good format.
Below is the output for the command aws ec2 describe-vpcs --regions us-east-1 | jq

Now, we'll write a shell script that pulls only the VPC IDs.

I used the aws CLI command and passed the variable that I stored in the name of REGION. We can write this script in different ways. I will demonstrate them in the upcoming articles.

Note: Make sure you stop the instance after the practice.