Skip to content

xpaulos/DevOps_week2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 

Repository files navigation

DevOps_week2

Week 2 | Tutorials for DevOps engineer Course

🧱 1. Your First Bash Script

# 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/bash line is called a shebang β€” it tells the system which interpreter to use. πŸ’‘ Follow above patern to create all the scripts below


πŸ“¦ 2. Variables

#!/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!


πŸ“₯ 3. User Input

#!/bin/bash

echo "What is your name?"
read user_name

echo "Welcome, $user_name! πŸŽ‰"

πŸ”€ 4. Conditionals (if / else)

#!/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

Common Comparison Operators

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

πŸ” 5. Loops

For Loop

#!/bin/bash

for i in 1 2 3 4 5; do
  echo "Iteration: $i"
done

While Loop

#!/bin/bash

counter=1

while [ $counter -le 5 ]; do
  echo "Count: $counter"
  counter=$((counter + 1))
done

🎯 Mini Project β€” Beginner

#!/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 "----------------------------------------"


🐚 Bash Tutorial β€” Intermediate

Writing Real-World Scripts


πŸ“Œ What You Should Already Know

Before this tutorial, make sure you're comfortable with variables, conditionals, and loops from the beginner guide.


πŸ”§ 1. Functions

#!/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 local inside functions to avoid polluting global scope.


πŸ“‚ 2. Working with Files & Directories

#!/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!"

πŸ”£ 3. Script Arguments

#!/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!"

Special Variables

Variable Meaning
$0 Script name
$1, $2 ... Positional arguments
$# Number of arguments
$@ All arguments
$? Exit code of last command

πŸ”„ 4. Error Handling

#!/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

πŸ“ 5. Logging

#!/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"

🎯 Mini Project β€” Intermediate

#!/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"

About

Week 2 | Tutorials for DevOps engineer Course

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors