# Shell Scripting

1. It is a set of commands written in an executable file to automate any task. The commands are converted then to machine-readable form to execute.

* Syntax:-

  **#!/bin/bash**

  **\<commands>**

2. The file to be created must be with extension ".sh" and provided with necessary execute permissions to the user running the script.
3. Navigate to the server and create a directory of your suitable name.
4. Create a shell script file. I have created here a AWS\_resource.sh file.&#x20;
5. In this project, I have chosen few resources to display and they are:-

* AWS EC2
* S3 Buckets
* AWS Lambda
* IAM users

6. The shell script file determine the AWS resource usage is as below:-

```
#!/bin/bash

path=/home/ubuntu/shell_script/script.sh

############################AWS Active Resources ####################################################################### echo "-----------------------------------------------------------------------------------------------------------------------------------------------------" 
#List S3 buckets
 echo "list aws s3 buckets:" 
aws s3 ls 
echo "-----------------------------------------------------------------------------------------------------------------------------------------------------" 
#list EC2 instances 
echo "list ec2 instances:" 
aws ec2 describe-instances 
echo "-----------------------------------------------------------------------------------------------------------------------------------------------------" 
#list lambda 
echo "list ec2 lambda:" 
aws lambda list-functions 
echo "-----------------------------------------------------------------------------------------------------------------------------------------------------" 
#list IAM users 
echo "list IAM users:" 
aws iam list-users
```

<figure><img src="https://389859271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FR9yUYsBi0Y1eeEpTEqSa%2Fuploads%2FH8SHT8WVk2Q1C5FpWO5s%2Fimage.png?alt=media&#x26;token=f39bb5d1-b422-47c3-b5e2-671f283674aa" alt=""><figcaption></figcaption></figure>

<figure><img src="https://389859271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FR9yUYsBi0Y1eeEpTEqSa%2Fuploads%2FqzgQuPokzMk1tE6z7RHK%2Fimage.png?alt=media&#x26;token=21ef08ca-ed6d-4a85-b288-d6a086929b04" alt=""><figcaption></figcaption></figure>

7. Now, let's add the server health monitoring where we have determined disk space and memory of the server.

```
############################## Health Check of Server #####################################################################

echo "-----------------------------------------------------------------------------------------------------------------------------------------------------"
echo "Disk usage of the server is:"
df -h
echo "-----------------------------------------------------------------------------------------------------------------------------------------------------"
M=`sudo cat /proc/meminfo | grep -i memtotal | awk '{print $2}'`
echo "Total memory of the server is:$M"

echo "Memory utilized in the server is:"
sudo free -h

############################################################################################################################################################
```

<figure><img src="https://389859271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FR9yUYsBi0Y1eeEpTEqSa%2Fuploads%2FY6mJjFzaSnZaJMYTI1mM%2Fimage.png?alt=media&#x26;token=ec8c1007-8109-4bc1-96b7-463e01056f0a" alt=""><figcaption></figcaption></figure>

8. Save the file and now give permission to the file with below command.

```
chmod 700 AWS_resource.sh
```

9. In this project we will send the mail to the user's account for which E-Mail services needs to be setup which we will see in next section.
10. &#x20;We need to put the output to a text file and then send the file content to the Email address. The shell script for this cause is:-

```
#!bin/bash

bash /home/ubuntu/shell_script/Aws_resource.sh 1>/home/ubuntu/shell_script/script.txt

cat script.txt | mail -s "AWS Resource cost and server monitoring" bandan5121998@gmail.com
```

<figure><img src="https://389859271-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FR9yUYsBi0Y1eeEpTEqSa%2Fuploads%2FE5jxd4ttITyFt3bsdBy7%2Fimage.png?alt=media&#x26;token=0ac82b8f-4bb8-4b3d-9f0a-b16c1e26a5d9" alt=""><figcaption></figcaption></figure>
