Sobes.tech
Junior — Middle

Can you give examples of scripts created in Bash and describe their functionality?

sobes.tech AI

Answer from AI

Examples of Bash scripts and their functionalities:

  1. Script for directory backup:
#!/bin/bash

SOURCE_DIR="/home/user/data"
BACKUP_DIR="/home/user/backup"

DATE=$(date +"%Y-%m-%d_%H-%M-%S")

mkdir -p "$BACKUP_DIR"
tar -czf "$BACKUP_DIR/backup_$DATE.tar.gz" "$SOURCE_DIR"
echo "Backup created: backup_$DATE.tar.gz"

This script archives the specified data directory and saves the archive with a timestamp.

  1. Script for monitoring disk usage:
#!/bin/bash

THRESHOLD=80
USAGE=$(df / | tail -1 | awk '{print $5}' | sed 's/%//')

if [ "$USAGE" -gt "$THRESHOLD" ]; then
  echo "Warning: Disk usage has exceeded $THRESHOLD%"
else
  echo "Disk usage is normal: $USAGE%"
fi

The script checks the disk usage percentage and warns if it exceeds the threshold.

  1. Script for automatic system update (on Debian/Ubuntu):
#!/bin/bash

sudo apt update && sudo apt upgrade -y

A simple script for updating system packages.

These scripts help automate routine tasks in system administration and DevOps.