Linux Shell Scripting for DevOps

Introduction to shell scripting :
A shell script is a program written in a shell programming language.
It is executed by the shell interpreter in Unix or Linux environments.
Shell scripts consist of a series of commands and instructions.
Shell scripting enables task automation and system administration.
It allows for file and directory manipulation.
Shell scripts facilitate the execution of complex operations.
Shell scripting is commonly used for repetitive or routine tasks.
It provides a way to streamline processes and increase efficiency.
Shell scripts can interact with files, directories, and system commands.
They are valuable tools for managing and maintaining systems.
simple use cases and examples for each topic:
- Shell Scripting Fundamentals:
Use case: Automating the backup of a directory. ๐๐พ
Example: Write a shell script that copies the contents of a specified directory to a backup directory with a timestamp appended to the backup folder's name.
#!/bin/bash
backup_dir="backup_$(date +%Y%m%d)"
mkdir $backup_dir
cp -r /path/to/source_directory/* $backup_dir
echo "Backup completed to $backup_dir"
Getting Started with Shell Scripting:
Use case: Printing a welcome message. ๐จ๏ธ๐
Example: Create a shell script that displays a personalized welcome message to the user when executed.
#!/bin/bash
read -p "Welcome! Please enter your name: " name
echo "Hello, $name! How's your 90daysofdevops challenge going?"
- Variables and Data Manipulation:
Use case: Calculating the total price of items. ๐ฐ๐๏ธ
Example: Write a shell script that prompts the user for item prices, stores them in variables, calculates the total price, and displays the result.
#!/bin/bash
read -p "Enter the price of the first item: " price1
read -p "Enter the price of the second item: " price2
total_price=$((price1 + price2))
echo "The total price is: $total_price"
- Control Flow and Decision Making:
Use case: Checking if a file exists. ๐๐
Example: Create a shell script that takes a filename as input and checks if the file exists. Display a message indicating whether the file exists or not.
#!/bin/bash
read -p "Please enter your name: " age
if [ $age -ge 18 ]; then
echo "Hello, You are an adult."
else
echo "Hello, You are a minor."
fi
- Loops and Iteration:
Use case: Processing a list of files. ๐๐
Example: Write a shell script that iterates over a directory, lists all the files, and performs a specific action on each file.
#!/bin/bash
fruits=("Apple" "Banana" "Orange" "Mango")
for fruit in "${fruits[@]}"; do
echo "Fruit: $fruit"
done
- Error Handling and Logging:
Use case: Handling a division by zero error. โ๐ซ
Example: Create a shell script that accepts two numbers as input, performs a division operation, and handles the error gracefully if the second number is zero.
#!/bin/bash
read -p "Enter the dividend: " dividend
read -p "Enter the divisor: " divisor
result=$((dividend / divisor))
echo "Result of division: $result" 2>/dev/null || echo "Error: Division by zero is not allowed."
Conclusion :
In conclusion, shell scripting is a powerful tool for automating tasks, managing systems, and manipulating files in DevOps. It boosts productivity and improves system management. Mastering shell scripting enhances skills and contributes to project success. Start scripting and unlock the power of automation in DevOps! ๐


