From 7f662b3e313780f564b878e4fb969973d4c2e201 Mon Sep 17 00:00:00 2001 From: Gege-source Date: Mon, 4 May 2026 01:39:37 +0100 Subject: [PATCH] feat: complete bash scripts --- README.md | 230 ++++++++++++++++++++++++ scripts/backup.sh | 22 +++ scripts/file_manager.sh | 45 +++++ scripts/logs/file_manager.log | 1 + scripts/logs/system_report_17-04-26.log | 15 ++ scripts/logs/user_info.log | 1 + scripts/process_monitor.sh | 28 +++ scripts/system_check.sh | 37 ++++ scripts/user_info.sh | 31 ++++ 9 files changed, 410 insertions(+) create mode 100644 README.md create mode 100755 scripts/backup.sh create mode 100755 scripts/file_manager.sh create mode 100644 scripts/logs/file_manager.log create mode 100644 scripts/logs/system_report_17-04-26.log create mode 100644 scripts/logs/user_info.log create mode 100755 scripts/process_monitor.sh create mode 100755 scripts/system_check.sh create mode 100755 scripts/user_info.sh diff --git a/README.md b/README.md new file mode 100644 index 0000000..07a3112 --- /dev/null +++ b/README.md @@ -0,0 +1,230 @@ +# 🚀 DevOps Bash Toolkit Assessment + +![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/your-username/devops-bash-toolkit/grade.yml) +![GitHub repo size](https://img.shields.io/github/repo-size/your-username/devops-bash-toolkit) +![GitHub last commit](https://img.shields.io/github/last-commit/your-username/devops-bash-toolkit) +![License](https://img.shields.io/badge/license-MIT-blue) + +--- + +## 📌 Overview + +This assignment tests your **DevOps fundamentals**: + +- Bash scripting +- Git workflow (branching, commits, pull requests) +- Automation mindset +- System monitoring and logging + +You will build a **real-world automation toolkit** and submit it via a **Pull Request (PR)**. + +--- + +## 📁 Project Structure + +```text +devops-bash-toolkit-assestment/ +│ +├── scripts/ +│ ├── user_info.sh +│ ├── system_check.sh +│ ├── file_manager.sh +│ ├── backup.sh +│ ├── process_monitor.sh +│ +├── run_all.sh # OPTIONAL (Bonus) +│ +├── README.md +``` + +🧑‍💻 Getting Started +1. Fork the Repository + +Click the Fork button on GitHub. + +2. Clone Your Fork +```bash +git clone +cd devops-bash-toolkit +``` + +3. Create a Feature Branch +```bash +git checkout -b feature/ +``` + +4. Complete the Scripts +Implement all required scripts inside: +```bash +scripts/ +``` + +5. Make Scripts Executable +```bash +chmod +x scripts/*.sh +``` + +6. Commit Your Work +```bash +git add . +git commit -m "feat: complete bash scripts" +``` + +7. Push to GitHub +```bash +git push origin feature/ +``` + +8. Create Pull Request + +Open a Pull Request to the main repository. + + +🧠 Assignment Tasks +-------------------------------------------------------------------------------- +🔹 A. user_info.sh +Requirements + +* Prompt the user for: + + * Name + + * Age + + * Country + +* Validate: + + * Age must be numeric + +* Output: + + * A greeting message + +* Age category: + + * Minor (<18) + + * Adult (18–65) + + * Senior (65+) + +* Handle missing or invalid input gracefully + +* Save output to: +```bash +logs/user_info.log +``` +--- +🔹 B. system_check.sh +Requirements + +* Display: + + * Disk usage (df -h) + + * Memory usage (free -m) + + * CPU load (uptime) + +* Warn if disk usage exceeds 80% + +* Save report to: +```bash +logs/system_report_.log +``` +* Count total running processes + +* Display top 5 memory-consuming processes +--- +🔹 C. file_manager.sh +Requirements + +* Support the following commands: + + * create + + * delete + + * list + + * rename + +* Example usage: +```bash +./file_manager.sh create file.txt +``` +* Prevent overwriting existing files + +* Log all actions to: +```bash +logs/file_manager.log +``` +--- +🔹 D. backup.sh +Requirements + +* Accept a directory as input + +* Validate that the directory exists + +* Create a compressed backup: +```bash +backup_.tar.gz +``` +* Store backups in: +```bash +backups/ +``` +* Keep only the last 5 backups (delete older ones) + +* Log backup activity +--- +⭐ E. process_monitor.sh(Optional Bonus) +Requirements + +* Accept a process name as input + +* Check if the process is running + +* If NOT running: + + * Attempt restart (or simulate restart) + +* Output: + + * Running + + * Stopped + + * Restarted + +* Use an array: +```bash +services=("nginx" "ssh" "docker") +``` +* Log monitoring results +--- +⭐ F. run_all.sh (Optional Bonus) +Requirements + +Provide an interactive menu: +1. Run all +2. System check +3. Backup +4. Exit +* Use functions to organize logic + +* Call scripts from the scripts/ directory + +* Include: + ```bash + set -euo pipefail + ``` +* Log all actions to: +```bash +logs/app.log +``` +* Handle script failures gracefully + +**Submission link:** [CLICK HERE](https://forms.gle/jrhpKjXsQXZxLopN6) diff --git a/scripts/backup.sh b/scripts/backup.sh new file mode 100755 index 0000000..681443c --- /dev/null +++ b/scripts/backup.sh @@ -0,0 +1,22 @@ +#!/bin/bash + +# Back up file Scripts directory +# Today's Timestam +timestamp=$(date +%d-%m-%y) + +# Path to target directory to be backed up +target_dir='/home/gege/devops-bash-toolkit-assestment/scripts' + +# Path to destination directory +destination_dir='/home/gege/scripts_backup_'$timestamp'.tar.gz' + +# Accept directory as input +if [[ ! -d $1 ]] then + echo "Argument is not a directory." + exit +else + cp -r ${target_dir} ${destination_dir} +fi + + + diff --git a/scripts/file_manager.sh b/scripts/file_manager.sh new file mode 100755 index 0000000..a072c0d --- /dev/null +++ b/scripts/file_manager.sh @@ -0,0 +1,45 @@ +#!/bin/bash +# Check the following actions run with this scrips + +action=$1 +filename=$2 + +# Using a case statement to check each action and possibility. +case $action in + create) + if [ -e $filename ]; then + echo "File already exists." + else + touch "$filename" + echo "File has been created." + fi + ;; + delete) + if [ -e $filename ]; then + rm "$filename" + echo "$filename has been deleted" + else + echo "$filename does not exist" + fi + ;; + list) + if [ -e $filename ]; then + ls + else + echo "$filename does not exist" + fi + ;; + rename) + if [ -e $filename ]; then + mv "$filename" "temp_file.tx" + echo "$filename has been renamed to temp_file" + else + echo "Failed to rename file" + fi + ;; + *) + echo "Unkown action: try: ./file_manager.sh {create} {delete} {list}" + ;; +esac + + diff --git a/scripts/logs/file_manager.log b/scripts/logs/file_manager.log new file mode 100644 index 0000000..70ca51d --- /dev/null +++ b/scripts/logs/file_manager.log @@ -0,0 +1 @@ +temp_file.tx has been deleted diff --git a/scripts/logs/system_report_17-04-26.log b/scripts/logs/system_report_17-04-26.log new file mode 100644 index 0000000..dc0dcf2 --- /dev/null +++ b/scripts/logs/system_report_17-04-26.log @@ -0,0 +1,15 @@ + total used free shared buff/cache available +Mem: 3855 446 3396 3 145 3408 +Swap: 1024 0 1024 + +System uptime: 20:50:12 up 2:40, 1 user, load average: 0.00, 0.00, 0.00 + +Disk usage is at 1 which is below the threshold of 80 + +Total running processes is: 27 + +0.5 +0.4 +0.3 +0.3 +0.3 diff --git a/scripts/logs/user_info.log b/scripts/logs/user_info.log new file mode 100644 index 0000000..517d9a9 --- /dev/null +++ b/scripts/logs/user_info.log @@ -0,0 +1 @@ +Welcome, Gege! You are 21 years old--an adult, and from Nigeria. diff --git a/scripts/process_monitor.sh b/scripts/process_monitor.sh new file mode 100755 index 0000000..fe07d2e --- /dev/null +++ b/scripts/process_monitor.sh @@ -0,0 +1,28 @@ +#!/bin/bash +# This script monitors a process + +services=("nginx" "ssh" "docker") +found=false + +# Iterate through array to check for a running process +for ps in ${services[@]}; do + if [[ $1 == "$ps" ]]; then + found=true + # check if process is running + if pgrep -x "$ps" > /dev/null; then + echo "$ps process is running." + else + echo "$ps process is not running." + echo "Attempting to install process..." + sudo apt install "$ps" + fi + # Exit the loop after find and processing service + break + fi +done + +# Notify if argument doesn't match the array +if ! $found; then + echo "$1 is not a valid argument" +fi + diff --git a/scripts/system_check.sh b/scripts/system_check.sh new file mode 100755 index 0000000..8282818 --- /dev/null +++ b/scripts/system_check.sh @@ -0,0 +1,37 @@ +#!/bin/bash +set -euo pipefail + +# Redirect input to a .log file +ts=$(date +%d-%m-%y) + +# Set Threshold +threshold=80 + +# Display Disk usage +diskuse=$(df -P / | awk 'NR>1 {print $5}' | sed 's/%//') + +# Display memory usage +free_memory=$(free -m) +echo "$free_memory" +echo "" + +# Display CPU loadtime +sys_uptime=$(uptime) +echo "System uptime: $sys_uptime" +echo "" +# Check if the disk usage exceeds the threshold +if [[ "$diskuse" -gt "$threshold" ]]; then + echo "Warning! Disk usage is at $diskuse% which exceeds the threshold of $threshold%" +else + echo "Disk usage is at $diskuse which is below the threshold of $threshold" +fi +echo "" + +# count total running processes +ps_count=$(ps aux | wc -l) +echo "Total running processes is: $ps_count" +echo "" + +# Display top 5 memory consuming processes +max_memory=$(ps aux | sort -k4 -nr | awk 'NR>=1 {print $4}' | head -n 5) +echo "$max_memory" diff --git a/scripts/user_info.sh b/scripts/user_info.sh new file mode 100755 index 0000000..914bb11 --- /dev/null +++ b/scripts/user_info.sh @@ -0,0 +1,31 @@ +#!/bin/bash +# User information requirement script +# prompt the user for name, age, and country. + +# Save output to logs/user_info.log + +while true; do + echo "Hello, please enter your name, age, country:" + read name age country + +# Redirect stdout and stderr to user_info.log +exec > logs/user_info.log 2>&1 + +# Verify strings are not empty and age is numeric. + if [[ -n "$name" ]] && [[ -n "$age" ]] && [[ -n "$country" ]] && [[ "$age" =~ ^[0-9]+$ ]]; then + # Age is numeric. Continue with the script. + if [[ "$age" -lt 18 ]]; then + # echo a greeting message to user. + echo "Welcome, $name! You are $age years old--a minor, and from $country." + elif [[ "$age" -ge 18 ]]; then + echo "Welcome, $name! You are $age years old--an adult, and from $country." + else + echo "Welcome, $name! You are over $age years old--a senior, and from $country." + break + fi + break + else + echo "Age must be a numeric value." + fi +done +