Week 2 | Tutorials for DevOps engineer Course
# Create a new script file
touch hello.sh
# Open it and add the following:
vim hello.sh#!/bin/bash
# This is a comment
echo "Hello, World! Welcome to Bash!"# Make it executable and run it
chmod +x hello.sh
./hello.shπ‘ The
#!/bin/bashline is called a shebang β it tells the system which interpreter to use. π‘ Follow above patern to create all the scripts below
#!/bin/bash
# Declaring variables (no spaces around =)
name="<Your Name>"
age=<your age>
city="<Your city>"
# Using variables
echo "Hello, my name is $name"
echo "I am $age years old and I live in $city"
β οΈ Never put spaces around=when assigning variables!
#!/bin/bash
echo "What is your name?"
read user_name
echo "Welcome, $user_name! π"#!/bin/bash
echo "Enter a number:"
read number
if [ $number -gt 10 ]; then
echo "The number is greater than 10"
elif [ $number -eq 10 ]; then
echo "The number is exactly 10"
else
echo "The number is less than 10"
fi| Operator | Meaning |
|---|---|
-eq |
Equal to |
-ne |
Not equal to |
-gt |
Greater than |
-lt |
Less than |
-ge |
Greater than or equal |
-le |
Less than or equal |
#!/bin/bash
for i in 1 2 3 4 5; do
echo "Iteration: $i"
done#!/bin/bash
counter=1
while [ $counter -le 5 ]; do
echo "Count: $counter"
counter=$((counter + 1))
done#!/bin/bash
# greeting.sh β A personalized greeting script
echo "Enter your name:"
read name
echo "Enter your favorite programming language:"
read lang
echo "----------------------------------------"
echo "π Hello, $name!"
echo "π $lang is a great choice to learn!"
echo "Keep coding and never give up!"
echo "----------------------------------------"Before this tutorial, make sure you're comfortable with variables, conditionals, and loops from the beginner guide.
#!/bin/bash
# Define a function
greet_user() {
local name=$1 # $1 = first argument passed to function
local role=$2 # $2 = second argument
echo "Hello, $name! Your role is: $role"
}
# Call the function
greet_user "Pavlos" "DevOps Engineer"
greet_user "Alice" "Developer"π‘ Use
localinside functions to avoid polluting global scope.
#!/bin/bash
TARGET_DIR="./project_backup"
# Check if directory exists, create if not
if [ ! -d "$TARGET_DIR" ]; then
mkdir -p "$TARGET_DIR"
echo "β
Directory created: $TARGET_DIR"
else
echo "π Directory already exists"
fi
# Loop through all .txt files in current directory
for file in *.txt; do
if [ -f "$file" ]; then
cp "$file" "$TARGET_DIR/"
echo "π Copied: $file"
fi
done
echo "π Backup complete!"#!/bin/bash
# Usage: ./deploy.sh <environment> <version>
ENVIRONMENT=$1
VERSION=$2
# Validate input
if [ -z "$ENVIRONMENT" ] || [ -z "$VERSION" ]; then
echo "β Usage: $0 <environment> <version>"
echo " Example: $0 production 1.4.2"
exit 1
fi
echo "π Deploying version $VERSION to $ENVIRONMENT..."
echo "β
Deployment complete!"| Variable | Meaning |
|---|---|
$0 |
Script name |
$1, $2 ... |
Positional arguments |
$# |
Number of arguments |
$@ |
All arguments |
$? |
Exit code of last command |
#!/bin/bash
# Exit immediately on error
set -e
# Exit on unset variable usage
set -u
# Catch errors in pipes
set -o pipefail
run_command() {
local cmd=$1
echo "βΆοΈ Running: $cmd"
if eval "$cmd"; then
echo "β
Success"
else
echo "β Failed with exit code: $?"
exit 1
fi
}
run_command "ls /tmp"
run_command "ls /nonexistent_folder" # This will trigger the error handler#!/bin/bash
LOG_FILE="./script.log"
TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")
log() {
local level=$1
local message=$2
echo "[$TIMESTAMP] [$level] $message" | tee -a "$LOG_FILE"
}
log "INFO" "Script started"
log "WARNING" "Low disk space detected"
log "ERROR" "Connection to database failed"
log "INFO" "Script finished"#!/bin/bash
# system_report.sh β Generates a system health report
set -euo pipefail
REPORT_FILE="system_report_$(date +%Y%m%d).txt"
log() { echo "[$(date '+%H:%M:%S')] $1" | tee -a "$REPORT_FILE"; }
print_section() { echo -e "\n===== $1 =====" | tee -a "$REPORT_FILE"; }
log "π₯οΈ System Report β $(date)"
print_section "OS Info"
uname -a | tee -a "$REPORT_FILE"
print_section "Disk Usage"
df -h | tee -a "$REPORT_FILE"
print_section "Memory Usage"
free -h | tee -a "$REPORT_FILE"
print_section "Top 5 CPU Processes"
ps aux --sort=-%cpu | head -6 | tee -a "$REPORT_FILE"
print_section "Current Users"
who | tee -a "$REPORT_FILE"
log "β
Report saved to: $REPORT_FILE"