Sobes.tech
Junior — Middle

Μπορείς να δώσεις παραδείγματα scripts που έχουν δημιουργηθεί σε Bash και να περιγράψεις τη λειτουργικότητά τους;

sobes.tech AI

Απάντηση από AI

Examples of Bash scripts and their functionality:

  1. Script for backing up a directory:
#!/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 compresses 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 exceeded $THRESHOLD%"
else
  echo "Disk usage is within normal limits: $USAGE%"
fi

This 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 to update system packages.

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