Goglides Dev 🌱

Cover image for Bash Scripting Tutorial
Roshan Thapa
Roshan Thapa

Posted on

Bash Scripting Tutorial

Linux Shell Script

In Linux, process automation relies heavily on shell scripting. This involves creating a file containing a series of commands that can be executed together

Introduction

Definition of Bash scripting

A bash script is a file containing a sequence of commands that are executed by the bash program line by line. It allows you to perform a series of actions, such as navigating to a specific directory, creating a folder, and launching a process using the command line.

By saving these commands in a script, you can repeat the same sequence of steps multiple times and execute them by running the script.

Advantages of Bash scripting

Bash scripting is a powerful and versatile tool for automating system administration tasks, managing system resources, and performing other routine tasks in Unix/Linux systems. Some advantages of shell scripting are:

  • Automation: Shell scripts allow you to automate repetitive tasks and processes, saving time and reducing the risk of errors that can occur with manual execution.
  • Portability: Shell scripts can be run on various platforms and operating systems, including Unix, Linux, macOS, and even Windows through the use of emulators or virtual machines.
  • Flexibility: Shell scripts are highly customizable and can be easily modified to suit specific requirements. They can also be combined with other programming languages or utilities to create more powerful scripts.
  • Accessibility: Shell scripts are easy to write and don't require any special tools or software. They can be edited using any text editor, and most operating systems have a built-in shell interpreter.
  • Integration: Shell scripts can be integrated with other tools and applications, such as databases, web servers, and cloud services, allowing for more complex automation and system management tasks.
  • Debugging: Shell scripts are easy to debug, and most shells have built-in debugging and error-reporting tools that can help identify and fix issues quickly.

Overview of Bash shell and command line interface

The terms "shell" and "bash" are used interchangeably. But there is a subtle difference between the two.

The term "shell" refers to a program that provides a command-line interface for interacting with an operating system. Bash (Bourne-Again SHell) is one of the most commonly used Unix/Linux shells and is the default shell in many Linux distributions.

How to Create and Execute Bash scripts

Script naming conventions

By naming convention, bash scripts end with .sh. However, bash scripts can run perfectly fine without the sh extension.

Adding the Shebang

Bash scripts start with a shebang. Shebang is a combination of bash # and bang ! followed by the bash shell path. This is the first line of the script. Shebang tells the shell to execute it via bash shell. Shebang is simply an absolute path to the bash interpreter.

Below is an example of the shebang statement.

#!/bin/bash
Enter fullscreen mode Exit fullscreen mode

You can find your bash shell path (which may vary from the above) using the command:

which bash
Enter fullscreen mode Exit fullscreen mode

Creating our first bash script

Our first script prompts the user to enter a path. In return, its contents will be listed.

Create a file named run_all.sh using the vi command. You can use any editor of your choice.

vi run_all.sh
Enter fullscreen mode Exit fullscreen mode

Add the following commands in your file and save it:

#!/bin/bash
echo "Today is " `date`

echo -e "\nenter the path to directory"
read the_path

echo -e "\n you path has the following files and folders: "
ls $the_path
Enter fullscreen mode Exit fullscreen mode

Script to print contents of a user supplied directory

Let's take a deeper look at the script line by line. I am displaying the same script again, but this time with line numbers.

  1 #!/bin/bash
  2 echo "Today is " `date`
  3
  4 echo -e "\nenter the path to directory"
  5 read the_path
  6
  7 echo -e "\n you path has the following files and folders: "
  8 ls $the_path
Enter fullscreen mode Exit fullscreen mode
  • Line #1: The shebang (#!/bin/bash) points toward the bash shell path.
  • Line #2: The echo command is displaying the current date and time on the terminal. Note that the date is in backticks.
  • Line #4: We want the user to enter a valid path.
  • Line #5: The read command reads the input and stores it in the variable the_path.
  • line #8: The ls command takes the variable with the stored path and displays the current files and folders.

Executing the bash script

To make the script executable, assign execution rights to your user using this command:

chmod u+x run_all.sh
Enter fullscreen mode Exit fullscreen mode

Here,

  • chmod modifies the ownership of a file for the current user :u.
  • +x adds the execution rights to the current user. This means that the user who is the owner can now run the script.
  • run_all.sh is the file we wish to run.

You can run the script using any of the mentioned methods:

  • sh run_all.sh
  • bash run_all.sh
  • ./run_all.sh ## Bash Scripting Basics Certainly! Bash scripting is a powerful tool for automating tasks and managing system configurations in Unix-like operating systems. Below are some key points about bash scripting along with examples:
  1. Shebang Line: This is the first line in a bash script and specifies the interpreter to be used to execute the script.
#!/bin/bash
Enter fullscreen mode Exit fullscreen mode
  1. Variables: Variables in bash scripts are declared without any specific data type and can store strings or numerical values.
name="John"
age=25
echo "My name is $name and I am $age years old."
Enter fullscreen mode Exit fullscreen mode
  1. Input: You can take user input in bash scripts using the read command.
echo "Enter your name: "
read name
echo "Hello, $name!"
Enter fullscreen mode Exit fullscreen mode
  1. Conditional Statements: Conditional statements like if, elif, and else are used for decision making in bash scripts. (-ge means greater or equal to)
if [ "$age" -ge 18 ]; then
    echo "You are an adult."
else
    echo "You are a minor."
fi
Enter fullscreen mode Exit fullscreen mode
  1. Loops: Bash supports for loops and while loops for iterating over a list of items or executing a block of code repeatedly.
for i in {1..5}; do
    echo "Count: $i"
done

counter=0
while [ $counter -lt 5 ]; do
    echo "Counter: $counter"
    ((counter++))
done
Enter fullscreen mode Exit fullscreen mode
  1. Functions: Bash functions allow you to group commands for reuse.
function greet {
    echo "Hello, $1!"
}

greet "Alice"
Enter fullscreen mode Exit fullscreen mode
  1. File Operations: Bash scripts can perform various file operations like reading, writing, and manipulating files.
# Read a file line by line
while IFS= read -r line; do
    echo "Line: $line"
done < input.txt

# Write to a file
echo "Hello World" > output.txt
Enter fullscreen mode Exit fullscreen mode
  1. Command Substitution: Bash allows the output of a command to replace the command itself.
current_date=$(date +%Y-%m-%d)
echo "Today's date is $current_date"
Enter fullscreen mode Exit fullscreen mode

These are just some basic aspects of bash scripting. With these building blocks, you can create scripts to automate various tasks and operations on your system.

Comments in bash scripting

Comments start with a # in bash scripting. This means that any line that begins with a # is a comment and will be ignored by the interpreter.

Comments are very helpful in documenting the code, and it is a good practice to add them to help others understand the code.

These are examples of comments:

# This is an example comment
# Both of these lines will be ignored by the interpreter
Enter fullscreen mode Exit fullscreen mode

Variables and data types in Bash

Variables let you store data. You can use variables to read, access, and manipulate data throughout your script.

There are no data types in Bash. In Bash, a variable is capable of storing numeric values, individual characters, or strings of characters.

In Bash, you can use and set the variable values in the following ways:

  1. Assign the value directly:
country=Nepal
Enter fullscreen mode Exit fullscreen mode
  1. Assign the value based on the output obtained from a program or command, using command substitution. Note that $ is required to access an existing variable's value.
same_country=$country
Enter fullscreen mode Exit fullscreen mode

This assigns the value of countryto the new variable same_country

To access the variable value, append $ to the variable name.

zaira@Zaira:~$ country=Nepal
zaira@Zaira:~$ echo $country
Pakistan
zaira@Zaira:~$ new_country=$country
zaira@Zaira:~$ echo $new_country
Pakistan
Enter fullscreen mode Exit fullscreen mode

Assigning and printing variable values

Variable naming conventions

In Bash scripting, the following are the variable naming conventions:

  1. Variable names should start with a letter or an underscore (_).
  2. Variable names can contain letters, numbers, and underscores (_).
  3. Variable names are case-sensitive.
  4. Variable names should not contain spaces or special characters.
  5. Use descriptive names that reflect the purpose of the variable.
  6. Avoid using reserved keywords, such as if, then, else, fi, and so on as variable names.

Here are some examples of valid variable names in Bash:

name
count
_var
myVar
MY_VAR

Enter fullscreen mode Exit fullscreen mode

And here are some examples of invalid variable names:

2ndvar (variable name starts with a number)
my var (variable name contains a space)
my-var (variable name contains a hyphen)

Enter fullscreen mode Exit fullscreen mode

Following these naming conventions helps make Bash scripts more readable and easier to maintain.

Input and output in Bash scripts

Gathering input

In this section, we'll discuss some methods to provide input to our scripts.

  1. Reading the user input and storing it in a variable

We can read the user input using the read command.

#!/bin/bash
echo "Today is " `date`

echo -e "\nenter the path to directory"
read the_path

echo -e "\nyour path has the following files and folders: "
ls $the_path
Enter fullscreen mode Exit fullscreen mode
  1. Reading from a file

This code reads each line from a file named input.txt and prints it to the terminal. We'll study while loops later in this article.

while read line
do
  echo $line
done < input.txt

Enter fullscreen mode Exit fullscreen mode
  1. Command line arguments

In a bash script or function, $1 denotes the initial argument passed, $2 denotes the second argument passed, and so forth.

This script takes a name as a command-line argument and prints a personalized greeting.

echo "Hello, $1!"

Enter fullscreen mode Exit fullscreen mode

We have supplied Zaira as our argument to the script.

#!/bin/bash
echo "Hello, $1!"
Enter fullscreen mode Exit fullscreen mode

The code for the script: greeting.sh

Displaying output

Here we'll discuss some methods to receive output from the scripts.

  1. Printing to the terminal:
echo "Hello, World!"

Enter fullscreen mode Exit fullscreen mode

This prints the text "Hello, World!" to the terminal.

  1. Writing to a file:
echo "This is some text." > output.txt

Enter fullscreen mode Exit fullscreen mode

This writes the text "This is some text." to a file named output.txt. Note that the >operator overwrites a file if it already has some content.

  1. Appending to a file:
echo "More text." >> output.txt

Enter fullscreen mode Exit fullscreen mode

This appends the text "More text." to the end of the file output.txt.

  1. Redirecting output:
ls > files.txt

Enter fullscreen mode Exit fullscreen mode

This lists the files in the current directory and writes the output to a file named files.txt. You can redirect output of any command to a file this way.

Basic Bash commands (echo, read, etc.)

Here is a list of some of the most commonly used bash commands:

  1. cd: Change the directory to a different location.
  2. ls: List the contents of the current directory.
  3. mkdir: Create a new directory.
  4. touch: Create a new file.
  5. rm: Remove a file or directory.
  6. cp: Copy a file or directory.
  7. mv: Move or rename a file or directory.
  8. echo: Print text to the terminal.
  9. cat: Concatenate and print the contents of a file.
  10. grep: Search for a pattern in a file.
  11. chmod: Change the permissions of a file or directory.
  12. sudo: Run a command with administrative privileges.
  13. df: Display the amount of disk space available.
  14. history: Show a list of previously executed commands.
  15. ps: Display information about running processes.

Conditional statements (if/else)

Expressions that produce a boolean result, either true or false, are called conditions. There are several ways to evaluate conditions, including if, if-else, if-elif-else, and nested conditionals.

*Syntax*:

if [[ condition ]];
then
    statement
elif [[ condition ]]; then
    statement 
else
    do this by default
fi
Enter fullscreen mode Exit fullscreen mode

Syntax of bash conditional statements

We can use logical operators such as AND -a and OR -o to make comparisons that have more significance.

if [ $a -gt 60 -a $b -lt 100 ]
Enter fullscreen mode Exit fullscreen mode

This statement checks if both conditions are true: a is greater than 60 AND b is less than 100.

Let's see an example of a Bash script that uses if, if-else, and if-elif-else statements to determine if a user-inputted number is positive, negative, or zero:

#!/bin/bash

echo "Please enter a number: "
read num

if [ $num -gt 0 ]; then
  echo "$num is positive"
elif [ $num -lt 0 ]; then
  echo "$num is negative"
else
  echo "$num is zero"
fi

Enter fullscreen mode Exit fullscreen mode

Script to determine if a number is positive, negative, or zero

The script first prompts the user to enter a number. Then, it uses an if statement to check if the number is greater than 0. If it is, the script outputs that the number is positive. If the number is not greater than 0, the script moves on to the next statement, which is an if-elif statement. Here, the script checks if the number is less than 0. If it is, the script outputs that the number is negative. Finally, if the number is neither greater than 0 nor less than 0, the script uses an else statement to output that the number is zero.

Looping and Branching in Bash

While loop

While loops check for a condition and loop until the condition remains true. We need to provide a counter statement that increments the counter to control loop execution.

In the example below, (( i += 1 )) is the counter statement that increments the value of i. The loop will run exactly 10 times.

#!/bin/bash
i=1
while [[ $i -le 10 ]] ; do
   echo "$i"
  (( i += 1 ))
done
Enter fullscreen mode Exit fullscreen mode

While loop that iterates 10 times.

For loop

The for loop, just like the while loop, allows you to execute statements a specific number of times. Each loop differs in its syntax and usage.

In the example below, the loop will iterate 5 times.

#!/bin/bash

for i in {1..5}
do
    echo $i
done
Enter fullscreen mode Exit fullscreen mode

For loop that iterates 5 times.

Case statements

In Bash, case statements are used to compare a given value against a list of patterns and execute a block of code based on the first pattern that matches. The syntax for a case statement in Bash is as follows:

case expression in
    pattern1)
        # code to execute if expression matches pattern1
        ;;
    pattern2)
        # code to execute if expression matches pattern2
        ;;
    pattern3)
        # code to execute if expression matches pattern3
        ;;
    *)
        # code to execute if none of the above patterns match expression
        ;;
esac

Enter fullscreen mode Exit fullscreen mode

Case statements syntax

Here, "expression" is the value that we want to compare, and "pattern1", "pattern2", "pattern3", and so on are the patterns that we want to compare it against.

The double semicolon ";;" separates each block of code to execute for each pattern. The asterisk "*" represents the default case, which executes if none of the specified patterns match the expression.

Let's see an example.

fruit="apple"

case $fruit in
    "apple")
        echo "This is a red fruit."
        ;;
    "banana")
        echo "This is a yellow fruit."
        ;;
    "orange")
        echo "This is an orange fruit."
        ;;
    *)
        echo "Unknown fruit."
        ;;
esac

Enter fullscreen mode Exit fullscreen mode

Example of case statement

In this example, since the value of "fruit" is "apple", the first pattern matches, and the block of code that echoes "This is a red fruit." is executed. If the value of "fruit" were instead "banana", the second pattern would match and the block of code that echoes "This is a yellow fruit." would execute, and so on. If the value of "fruit" does not match any of the specified patterns, the default case is executed, which echoes "Unknown fruit."

How to Schedule Scripts using cron

Cron is a powerful utility for job scheduling that is available in Unix-like operating systems. By configuring cron, you can set up automated jobs to run on a daily, weekly, monthly, or specific time basis. The automation capabilities provided by cron play a crucial role in Linux system administration.

Below is the syntax to schedule crons:

# Cron job example
* * * * * sh /path/to/script.sh
Enter fullscreen mode Exit fullscreen mode

Here, the *s represent minute(s) hour(s) day(s) month(s) weekday(s), respectively.

Below are some examples of scheduling cron jobs.

SCHEDULE

DESCRIPTION

EXAMPLE

0 0 * * *

Run a script at midnight every day

0 0 * * * /path/to/script.sh

*/5 * * * *

Run a script every 5 minutes

*/5 * * * * /path/to/script.sh

0 6 * * 1-5

Run a script at 6 am from Monday to Friday

0 6 * * 1-5 /path/to/script.sh

0 0 1-7 * *

Run a script on the first 7 days of every month

0 0 1-7 * * /path/to/script.sh

0 12 1 * *

Run a script on the first day of every month at noon

0 12 1 * * /path/to/script.sh

Using crontab

The crontab utility is used to add and edit the cron jobs.

crontab -l lists the already scheduled scripts for a particular user.

You can add and edit the cron through crontab -e.

You can read more about corn jobs in my other article here.

How to Debug and Troubleshoot Bash Scripts

Debugging and troubleshooting are essential skills for any Bash scripter. While Bash scripts can be incredibly powerful, they can also be prone to errors and unexpected behavior. In this section, we will discuss some tips and techniques for debugging and troubleshooting Bash scripts.

Set the set -x option

One of the most useful techniques for debugging Bash scripts is to set the set -x option at the beginning of the script. This option enables debugging mode, which causes Bash to print each command that it executes to the terminal, preceded by a + sign. This can be incredibly helpful in identifying where errors are occurring in your script.

#!/bin/bash

set -x

# Your script goes here

Enter fullscreen mode Exit fullscreen mode

Check the exit code

When Bash encounters an error, it sets an exit code that indicates the nature of the error. You can check the exit code of the most recent command using the $? variable. A value of 0 indicates success, while any other value indicates an error.

#!/bin/bash

# Your script goes here

if [ $? -ne 0 ]; then
    echo "Error occurred."
fi

Enter fullscreen mode Exit fullscreen mode

Use echo statements

Another useful technique for debugging Bash scripts is to insert echo statements throughout your code. This can help you identify where errors are occurring and what values are being passed to variables.

#!/bin/bash

# Your script goes here

echo "Value of variable x is: $x"

# More code goes here

Enter fullscreen mode Exit fullscreen mode

Use the set -e option

If you want your script to exit immediately when any command in the script fails, you can use the set -e option. This option will cause Bash to exit with an error if any command in the script fails, making it easier to identify and fix errors in your script.

#!/bin/bash

set -e

# Your script goes here

Enter fullscreen mode Exit fullscreen mode

Troubleshooting crons by verifying logs

We can troubleshoot crons using the log files. Logs are maintained for all the scheduled jobs. You can check and verify in logs if a specific job ran as intended or not.

For Ubuntu/Debian, you can find cronlogs at:

/var/log/syslog

Enter fullscreen mode Exit fullscreen mode

The location varies for other distributions.

A cron job log file can look like this:

2022-03-11 00:00:01 Task started
2022-03-11 00:00:02 Running script /path/to/script.sh
2022-03-11 00:00:03 Script completed successfully
2022-03-11 00:05:01 Task started
2022-03-11 00:05:02 Running script /path/to/script.sh
2022-03-11 00:05:03 Error: unable to connect to database
2022-03-11 00:05:03 Script exited with error code 1
2022-03-11 00:10:01 Task started
2022-03-11 00:10:02 Running script /path/to/script.sh
2022-03-11 00:10:03 Script completed successfully

Enter fullscreen mode Exit fullscreen mode

Some Example of Shell Script

1. Automated Deployment Script:

This script automates the deployment process of a web application. It pulls the latest changes from the version control system, builds the application, and deploys it to the production server.

#!/bin/bash

# Pull latest changes from Git repository
git pull origin master

# Build the application
mvn clean install

# Stop the application server
systemctl stop myapp.service

# Deploy the new version
cp target/myapp.war /var/www/html

# Start the application server
systemctl start myapp.service

echo "Deployment completed successfully."
Enter fullscreen mode Exit fullscreen mode

2. Continuous Integration Script:

This script runs continuous integration tests for a project whenever changes are pushed to the repository. It runs unit tests, linters, and code quality checks.

`#!/bin/bash

# Run unit tests
npm test

# Run linter
eslint .

# Run code quality checks
sonar-scanner -Dsonar.projectKey=myproject -Dsonar.sources=src` 
Enter fullscreen mode Exit fullscreen mode

3. Infrastructure Provisioning Script:

This script automates the provisioning of infrastructure resources using a cloud provider's API. It creates virtual machines, sets up networking, and installs necessary software.

`#!/bin/bash

# Provision virtual machine
aws ec2 run-instances --image-id ami-12345678 --instance-type t2.micro --key-name mykeypair --security-group-ids sg-12345678 --subnet-id subnet-12345678

# Configure networking
aws ec2 create-vpc --cidr-block 10.0.0.0/16
aws ec2 create-subnet --vpc-id vpc-12345678 --cidr-block 10.0.1.0/24

# Install software
ssh user@hostname "sudo apt-get update && sudo apt-get install -y nginx"` 
Enter fullscreen mode Exit fullscreen mode

4. Backup and Restore Script:

This script automates the backup and restoration of critical data, such as database backups, configuration files, and user data.

`#!/bin/bash

# Backup database
mysqldump -u username -p password mydatabase > backup.sql

# Backup configuration files
tar -czf config_backup.tar.gz /etc/nginx /etc/apache2

# Backup user data
rsync -avz /home/user/data/ /backup/data/

# Restore database
mysql -u username -p password mydatabase < backup.sql

# Restore configuration files
tar -xzf config_backup.tar.gz -C /` 
Enter fullscreen mode Exit fullscreen mode

5. Monitoring Script:

This script automates monitoring tasks, such as checking server health, monitoring system resources, and sending alerts.

`#!/bin/bash

# Check server uptime
uptime

# Check memory usage
free -m

# Check disk usage
df -h

# Send alert if CPU usage exceeds threshold
cpu_usage=$(top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1}')
if [ $(echo "$cpu_usage > 80" | bc) -eq 1 ]; then
    echo "CPU usage is high. Alert!"
    # Send alert command
fi`
Enter fullscreen mode Exit fullscreen mode

6. Log Rotation Script:

This script automates log rotation to prevent log files from consuming too much disk space. It compresses old log files and deletes logs older than a specified period.

#!/bin/bash

LOG_DIR="/var/log/myapp"

MAX_AGE_DAYS=7

# Compress old log files

find $LOG_DIR -type  f  -name  "*.log"  -mtime  +$MAX_AGE_DAYS -exec  gzip  {} \;
# Delete logs older than MAX_AGE_DAYS

find $LOG_DIR -type  f  -name  "*.log.gz"  -mtime  +$MAX_AGE_DAYS -exec  rm  {} \;
Enter fullscreen mode Exit fullscreen mode

7. Git Repository Maintenance Script:

This script automates routine maintenance tasks for Git repositories, such as cleaning up branches, checking for untracked files, and fetching upstream changes.

#!/bin/bash

# Clean up local branches

git fetch --prune

# Check for untracked files

untracked_files=$(git ls-files --others --exclude-standard)

if [ -n "$untracked_files" ]; then

echo "Untracked files found:"

echo "$untracked_files"

fi

# Fetch upstream changes

git fetch origin
Enter fullscreen mode Exit fullscreen mode

8. Docker Container Management Script:

This script automates management tasks for Docker containers, such as starting, stopping, and updating containers.

#!/bin/bash

# Start Docker container

docker start  mycontainer

# Stop Docker container

docker stop  mycontainer

# Update Docker container

docker pull  myimage:latest

docker stop  mycontainer

docker rm  mycontainer

docker run  -d  --name  mycontainer  myimage:latest
Enter fullscreen mode Exit fullscreen mode

9. SSL Certificate Renewal Script:

This script automates the renewal of SSL certificates for web servers using Let's Encrypt or Certbot.

#!/bin/bash

# Renew SSL certificate

certbot renew

# Restart web server to apply changes

systemctl restart nginx
Enter fullscreen mode Exit fullscreen mode

10. Deployment Rollback Script:

This script automates the rollback of a deployment in case of issues or errors.

#!/bin/bash

# Rollback to previous version

mv /var/www/html/backup_rollback.tar.gz /var/www/html/current

service apache2 restart
Enter fullscreen mode Exit fullscreen mode

11. Service Monitoring Script:

This script checks the status of specified services and restarts them if they are not running.

#!/bin/bash

services=("nginx" "mysql" "apache2")

for service in "${services[@]}"; do

if systemctl is-active --quiet "$service"; then

echo "$service is running."

else

echo "$service is not running. Restarting..."

systemctl restart "$service"

fi

done
Enter fullscreen mode Exit fullscreen mode

12. Database Backup Script with Rotation:

This script performs backups of a MySQL database and rotates old backups to prevent disk space issues.

#!/bin/bash

DB_USER="username"

DB_PASSWORD="password"

DB_NAME="database_name"

BACKUP_DIR="/var/backups/mysql"

MAX_BACKUPS=5
mkdir -p "$BACKUP_DIR"

# Backup database

mysqldump -u "$DB_USER" -p"$DB_PASSWORD" "$DB_NAME" > "$BACKUP_DIR/backup_$(date +%Y%m%d_%H%M%S).sql"

# Remove old backups exceeding MAX_BACKUPS

while [ "$(ls -1 "$BACKUP_DIR" | wc -l)" -gt "$MAX_BACKUPS" ]; do

oldest_backup=$(ls -t "$BACKUP_DIR" | tail -1)

rm "$BACKUP_DIR/$oldest_backup"

done
Enter fullscreen mode Exit fullscreen mode

13. Containerized Application Deployment Script:

This script automates the deployment of a containerized application using Docker Compose.

#!/bin/bash

COMPOSE_FILE="docker-compose.yml"

docker-compose -f "$COMPOSE_FILE" up -d --build
Enter fullscreen mode Exit fullscreen mode

14. Kubernetes Deployment Script:

This script deploys an application to a Kubernetes cluster using kubectl.

#!/bin/bash

KUBE_NAMESPACE="default"

KUBE_DEPLOYMENT_FILE="deployment.yaml"

kubectl apply -f "$KUBE_DEPLOYMENT_FILE" -n "$KUBE_NAMESPACE"
Enter fullscreen mode Exit fullscreen mode

15. Network Configuration Script:

This script configures network settings on a Linux server.

#!/bin/bash

INTERFACE="eth0"

IP_ADDRESS="192.168.1.100"

NETMASK="255.255.255.0"

GATEWAY="192.168.1.1"

ip addr add "$IP_ADDRESS"/24 dev "$INTERFACE"

ip link set dev "$INTERFACE" up

ip route add default via "$GATEWAY" dev "$INTERFACE"
Enter fullscreen mode Exit fullscreen mode

16. Cron Job Monitoring Script:

This script checks the status of cron jobs and sends an email notification if any job fails to run successfully.

#!/bin/bash

CRON_LOG="/var/log/cron.log"

if grep -q "ERROR" "$CRON_LOG"; then

mail -s "Cron Job Failure" [email protected] <<< "One or more cron jobs failed. Check $CRON_LOG for details."

fi
Enter fullscreen mode Exit fullscreen mode

17. AWS S3 Backup Script:

This script automates the backup of files to Amazon S3 using the AWS CLI.

#!/bin/bash

SOURCE_DIR="/path/to/source"

S3_BUCKET="s3://my-bucket"

aws s3 sync "$SOURCE_DIR" "$S3_BUCKET"
Enter fullscreen mode Exit fullscreen mode

18. Terraform Deployment Script:

This script deploys infrastructure using Terraform.

#!/bin/bash

TERRAFORM_DIR="/path/to/terraform"

cd "$TERRAFORM_DIR"

terraform init

terraform apply --auto-approve
Enter fullscreen mode Exit fullscreen mode

19. Docker Image Cleanup Script:

This script removes unused Docker images and containers to free up disk space.

#!/bin/bash

docker image prune -a

docker container prune
Enter fullscreen mode Exit fullscreen mode

20. Server Configuration Backup Script:

This script backs up server configuration files to a specified directory.

#!/bin/bash

CONFIG_DIR="/etc"

BACKUP_DIR="/path/to/backup"

tar -czf "$BACKUP_DIR/config_backup_$(date +%Y%m%d_%H%M%S).tar.gz" "$CONFIG_DIR"
Enter fullscreen mode Exit fullscreen mode

21. Health Check Endpoint Script:

This script creates a simple HTTP endpoint for health checks, useful in containerized environments.

#!/bin/bash

while true; do

echo -e "HTTP/1.1 200 OK\n\n$(date)" | nc -l -p 8080 -q 1

done
Enter fullscreen mode Exit fullscreen mode

22. Jenkins Job Trigger Script:

This script triggers a Jenkins job using the Jenkins CLI.

#!/bin/bash

JENKINS_URL="http://jenkins.example.com"

JOB_NAME="my-job"

java -jar jenkins-cli.jar -s "$JENKINS_URL" build "$JOB_NAME"
Enter fullscreen mode Exit fullscreen mode

23. Git Branch Cleanup Script:

This script deletes merged Git branches that are no longer needed.

#!/bin/bash

git branch --merged | grep -v "master" | xargs -n 1 git branch -d
Enter fullscreen mode Exit fullscreen mode

24. Certificate Expiry Check Script:

This script checks the expiry dates of SSL certificates and sends an alert if any certificate is close to expiration.

#!/bin/bash

certs_dir="/etc/ssl/certs"

threshold_days=30

for cert_file in "$certs_dir"/*.crt; do

days_remaining=$(openssl x509 -noout -enddate -in "$cert_file" | awk -F= '{print $2}' | xargs -I {} bash -c 'date  -d  "{}"  +%s  -  $(date +%s) | bc')

if [ "$days_remaining" -lt "$threshold_days" ]; then

echo "Certificate $cert_file is expiring soon. Days remaining: $days_remaining"

# Send alert

fi

done
Enter fullscreen mode Exit fullscreen mode

25. Git Repository Cloning Script:

This script automates the cloning of multiple Git repositories.

#!/bin/bash

repositories=("https://github.com/user/repo1.git" "https://github.com/user/repo2.git")

for repo in "${repositories[@]}"; do

git clone "$repo"

done
Enter fullscreen mode Exit fullscreen mode

26. Log File Parsing Script:

This script parses log files and extracts specific information or performs analysis.

#!/bin/bash

LOG_FILE="/var/log/application.log"

# Extract error messages

grep "ERROR" "$LOG_FILE"

# Count occurrences of a specific event

grep -c "EVENT_NAME" "$LOG_FILE"
Enter fullscreen mode Exit fullscreen mode

27. Kubernetes Pod Restart Script:

This script restarts Kubernetes pods based on specified criteria, such as labels or namespaces.

#!/bin/bash

NAMESPACE="my-namespace"

LABEL_SELECTOR="app=my-app"

kubectl delete pods -n "$NAMESPACE" --selector="$LABEL_SELECTOR"
Enter fullscreen mode Exit fullscreen mode

28. Docker Container Resource Monitoring Script:

This script monitors resource usage of Docker containers and sends alerts if usage exceeds thresholds.

#!/bin/bash

CONTAINER_ID="container_id"

CPU_THRESHOLD=80

MEMORY_THRESHOLD=90

cpu_usage=$(docker stats --no-stream --format "{{.CPUPerc}}" "$CONTAINER_ID" | tr -d '%')

memory_usage=$(docker stats --no-stream --format "{{.MemPerc}}" "$CONTAINER_ID" | tr -d '%')

if (( $(echo "$cpu_usage > $CPU_THRESHOLD" | bc -l) )); then

echo "CPU usage exceeds threshold: $cpu_usage%"

# Send alert

fi

if (( $(echo "$memory_usage > $MEMORY_THRESHOLD" | bc -l) )); then

echo "Memory usage exceeds threshold: $memory_usage%"

# Send alert

fi
Enter fullscreen mode Exit fullscreen mode

29. Automated Database Migration Script:

This script automates database schema migrations using tools like Flyway or Liquibase.


#!/bin/bash

FLYWAY_CONFIG="/path/to/flyway.conf"

flyway -configFile="$FLYWAY_CONFIG" migrate
Enter fullscreen mode Exit fullscreen mode

30. Environment Variable Management Script:

This script manages environment variables for applications deployed in various environments (dev, staging, production).


#!/bin/bash

ENVIRONMENT="production"

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

export DB_HOST="prod-db.example.com"

export DB_PORT="5432"

export LOG_LEVEL="INFO"

elif [ "$ENVIRONMENT" == "dev" ]; then

export DB_HOST="dev-db.example.com"

export DB_PORT="5432"

export LOG_LEVEL="DEBUG"

fi
Enter fullscreen mode Exit fullscreen mode

31. Automated SSL Certificate Renewal and Deployment Script:

This script automates the renewal of SSL certificates using Let's Encrypt/Certbot and deploys them to a web server.


#!/bin/bash

DOMAIN="example.com"

WEBROOT="/var/www/html"

NGINX_SERVICE="nginx"

# Renew SSL certificate

certbot renew --nginx --quiet

# Deploy renewed certificate to web server

ln -sf /etc/letsencrypt/live/$DOMAIN/fullchain.pem /etc/nginx/ssl/$DOMAIN.crt

ln -sf /etc/letsencrypt/live/$DOMAIN/privkey.pem /etc/nginx/ssl/$DOMAIN.key

# Reload Nginx to apply changes

systemctl reload $NGINX_SERVICE
Enter fullscreen mode Exit fullscreen mode

32. Automated Database Backup and Upload to AWS S3 Script:

This script automates database backups and uploads them to Amazon S3 for secure storage.


#!/bin/bash

DB_USER="username"

DB_PASSWORD="password"

DB_NAME="database_name"

BACKUP_DIR="/path/to/backups"

S3_BUCKET="s3://my-bucket"

# Backup database

mysqldump -u  "$DB_USER"  -p"$DB_PASSWORD"  "$DB_NAME" > "$BACKUP_DIR/backup_$(date +%Y%m%d_%H%M%S).sql"

# Upload backup to S3

aws s3  cp  "$BACKUP_DIR"  "$S3_BUCKET"  --recursive
Enter fullscreen mode Exit fullscreen mode

33. Automated Continuous Integration and Deployment Script:

This script automates the continuous integration and deployment of a project using GitLab CI/CD.

yaml


# .gitlab-ci.yml

stages:

- build

- test

- deploy

build:

stage: build

script:

- mvn clean install

test:

stage: test

script:

- mvn test

deploy:

stage: deploy

script:

- ./deploy.sh
Enter fullscreen mode Exit fullscreen mode

34. Infrastructure as Code Provisioning and Deployment Script:

This script automates the provisioning and deployment of infrastructure using Terraform and Ansible.


#!/bin/bash

TERRAFORM_DIR="/path/to/terraform"

ANSIBLE_DIR="/path/to/ansible"


# Provision infrastructure with Terraform

cd "$TERRAFORM_DIR"

terraform init

terraform apply  --auto-approve

# Deploy application with Ansible

cd "$ANSIBLE_DIR"

ansible-playbook deploy.yml
Enter fullscreen mode Exit fullscreen mode

35. Kubernetes Cluster Deployment Script:

This script automates the deployment of a Kubernetes cluster using tools like kops or kubespray.


#!/bin/bash

CLUSTER_NAME="my-cluster"

KOPS_STATE_STORE="s3://my-kops-state-store"

# Create Kubernetes cluster with kops

kops create cluster --name "$CLUSTER_NAME" --state="$KOPS_STATE_STORE" --zones=us-east-1a,us-east-1b

# Update DNS records

kops update cluster --name "$CLUSTER_NAME" --state="$KOPS_STATE_STORE" --yes
Enter fullscreen mode Exit fullscreen mode

Top comments (0)