From c9e3bf379bf361e391c8eb62018aadd4a7e2c503 Mon Sep 17 00:00:00 2001 From: theisraelolaleye Date: Fri, 10 Apr 2026 19:58:43 +0100 Subject: [PATCH] bash scripting assignment --- backups/backup_scripts_20260401_020415.tar.gz | Bin 0 -> 2090 bytes logs/backup.log | 1 + logs/file_manager.log | 4 + logs/process_monitor.log | 1 + logs/system_report_20260401_020414.log | 54 ++++++++ logs/user_info.log | 1 + run_all.sh | 98 +++++++++++++++ scripts/backup.sh | 44 +++++++ scripts/file_manager.sh | 119 ++++++++++++++++++ scripts/logs/user_info.log | 4 + scripts/process_monitor.sh | 35 ++++++ scripts/system_check.sh | 15 +++ scripts/user_info.sh | 42 +++++++ 13 files changed, 418 insertions(+) create mode 100644 backups/backup_scripts_20260401_020415.tar.gz create mode 100644 logs/backup.log create mode 100644 logs/file_manager.log create mode 100644 logs/process_monitor.log create mode 100644 logs/system_report_20260401_020414.log create mode 100644 logs/user_info.log create mode 100755 run_all.sh create mode 100755 scripts/backup.sh create mode 100755 scripts/file_manager.sh 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/backups/backup_scripts_20260401_020415.tar.gz b/backups/backup_scripts_20260401_020415.tar.gz new file mode 100644 index 0000000000000000000000000000000000000000..22ab028de45b25cedeb11f7f1c1fc8a649b23768 GIT binary patch literal 2090 zcmV+_2-Wu=iwFP@Z_H@`1MOOEQ`<%m&S(9Kjbhh!iu{IcQWJZI5ZW*al)=elI+S{Z zbhb4}I`vL+AV7b6_wFQ1mhBKypp(`&V`QE7_Vu*S?ip?|CkT1%$pemhy}s3MgS>Xs zwcc#0YdoOQZge*5oz7;v1@%U2v(}Z4 z+q|G$fh!HbHbVmXkHz1NqGuFsD8A_y54y#HuH9%F@);sIH^(FNe6-5rc&r}KNeQ$vV)vFd>P;j zg@Ye?ggF*WO*bOnCcj)3mcdJCynsq&Cey-AFsAIXryn>zWx7V(3~>)IK`f=-u_HGW z)SS%C4~XvwHbSbj=M8aPx&bmSFk;k$Bo0;hjYiVD!Ygn|OvZt_wsOAW=zJ!4&%3xN z7J=4hZC_&fzx@vXlab{Ak>iq~XZmOsyKe}%CI7dY&1RbaTXoF;TbuRvI{!b$_{RJ{ zw_WZN0!%A(sDF0^h!%k31DC12P=_lEGkZv`ZQ>HVxDF4m84>y622M;i=BH_b+q*z1 zF5%sD3Iq?u0)*Yw@FL7TRh(Bn6#}7}n^X#j^dnY)Pd=%YXp2QXbghl**WGB@wWUNn)y?m|hDmJsX&^j-?KJmfQp^N=G(xLz*IR0(f>6(yrs> z>~Vkba3n2I?k>5MS(B+&RHB`)Su#T7H5g!l`DJmHs5GZRMb}JbO`Iu-ivtzXVc{yX z{AO4v%TQsYieAwk7T**Pii1iOU!>~zbIKwx!$~h2^I9RxT}1#Ffnj|Xg<^J{4#qA{ zl~Jo@YNq!>BmGNmbsXhS7?JrvHHYd5kG|Zt*UCG_-lmoFG}rs>{)2`Yc`qI{K$22mz$r?p>%lM zZ%8YvFN6h%@-sXMgFraoC?^nYd~ z_|Gq~sqlu@gjnZ~eEx@6-^k(r)@Emo|Bo`h2LE60yx$uR-tQb73FKIo`j?gi@s?sF zpe#fPN!i?d>Bum}_JL#oyJAnmv19`VC=DvShC@#ji1gqW$|3H~P^r-J(`FO7oa9jRwwX*;J(FW`Y8_mu-|3Aj~ z#@~PP+ePMcPT17J>0Vj)$Buu2RXXQb$?GW-m-a+) zmcybxyS4-Zjv`bvPP8)(>r%tJa2SL%2#BqVmS^C2kt>49L|_nkM3= 'backups/backup_scripts_20260401_020415.tar.gz' diff --git a/logs/file_manager.log b/logs/file_manager.log new file mode 100644 index 0000000..4d4d968 --- /dev/null +++ b/logs/file_manager.log @@ -0,0 +1,4 @@ +[2026-04-01 02:04:13] Created file: 'temp_test.txt' +[2026-04-01 02:04:13] Renamed 'temp_test.txt' to 'temp_test_renamed.txt' +[2026-04-01 02:04:13] Listed directory: '.' +[2026-04-01 02:04:13] Deleted: 'temp_test_renamed.txt' diff --git a/logs/process_monitor.log b/logs/process_monitor.log new file mode 100644 index 0000000..0a5149e --- /dev/null +++ b/logs/process_monitor.log @@ -0,0 +1 @@ +[2026-04-01 02:04:15] Process 'bash' status: Running diff --git a/logs/system_report_20260401_020414.log b/logs/system_report_20260401_020414.log new file mode 100644 index 0000000..684564c --- /dev/null +++ b/logs/system_report_20260401_020414.log @@ -0,0 +1,54 @@ +System Check Report - Wed 1 Apr 2026 02:04:14 WAT +======================================== + +[Disk Usage] +Filesystem Size Used Avail Capacity iused ifree %iused Mounted on +/dev/disk3s1s1 460Gi 12Gi 20Gi 37% 455k 211M 0% / +devfs 218Ki 218Ki 0Bi 100% 754 0 100% /dev +/dev/disk3s6 460Gi 7.0Gi 20Gi 26% 7 211M 0% /System/Volumes/VM +/dev/disk3s2 460Gi 8.1Gi 20Gi 29% 1.4k 211M 0% /System/Volumes/Preboot +/dev/disk3s4 460Gi 3.0Mi 20Gi 1% 66 211M 0% /System/Volumes/Update +/dev/disk1s2 500Mi 6.0Mi 483Mi 2% 1 4.9M 0% /System/Volumes/xarts +/dev/disk1s1 500Mi 5.8Mi 483Mi 2% 31 4.9M 0% /System/Volumes/iSCPreboot +/dev/disk1s3 500Mi 884Ki 483Mi 1% 63 4.9M 0% /System/Volumes/Hardware +/dev/disk3s5 460Gi 412Gi 20Gi 96% 6.7M 211M 3% /System/Volumes/Data +map auto_home 0Bi 0Bi 0Bi 100% 0 0 - /System/Volumes/Data/home +/dev/disk5s1 16Gi 15Gi 471Mi 98% 498k 4.8M 9% /Library/Developer/CoreSimulator/Volumes/iOS_21A342 +/dev/disk7s1 17Gi 16Gi 438Mi 98% 608k 4.5M 12% /Library/Developer/CoreSimulator/Volumes/iOS_23D8133 +/dev/disk9s1 385Mi 308Mi 75Mi 81% 552 768k 0% /Volumes/Redis Insight 2.70.1-arm64 +/Applications/Pi.app/Wrapper 460Gi 411Gi 17Gi 96% 6.7M 180M 4% /private/var/folders/wb/mzs_c3rs3dxg3ny4_qjb8jr80000gn/X/3D6AE059-CE29-57AE-83C8-F52C55B49D47 + +[Memory Usage] +Mach Virtual Memory Statistics: (page size of 16384 bytes) +Pages free: 3607. +Pages active: 87622. +Pages inactive: 82929. +Pages speculative: 3702. +Pages throttled: 0. +Pages wired down: 128762. +Pages purgeable: 116. +"Translation faults": 8593002225. +Pages copy-on-write: 133987569. +Pages zero filled: 1387209860. +Pages reactivated: 3309616018. +Pages purged: 195006250. +File-backed pages: 64104. +Anonymous pages: 110149. +Pages stored in compressor: 1127316. +Pages occupied by compressor: 180828. +Decompressions: 4188604159. +Compressions: 4461894951. +Pageins: 388002623. +Pageouts: 8041712. +Swapins: 175807753. +Swapouts: 184713802. + +[CPU Load] + 2:04 up 15 days, 17:21, 3 users, load averages: 2.29 2.17 3.60 + +[Disk Usage Warnings (>80%)] +WARNING: /dev/disk3s5 is at 96% usage (mount: /System/Volumes/Data)\nWARNING: /dev/disk5s1 is at 98% usage (mount: /Library/Developer/CoreSimulator/Volumes/iOS_21A342)\nWARNING: /dev/disk7s1 is at 98% usage (mount: /Library/Developer/CoreSimulator/Volumes/iOS_23D8133)\nWARNING: /dev/disk9s1 is at 81% usage (mount: /Volumes/Redis)\nWARNING: /Applications/Pi.app/Wrapper is at 96% usage (mount: /private/var/folders/wb/mzs_c3rs3dxg3ny4_qjb8jr80000gn/X/3D6AE059-CE29-57AE-83C8-F52C55B49D47)\n +[Total Running Processes] +512 + +[Top 5 Memory-Consuming Processes] diff --git a/logs/user_info.log b/logs/user_info.log new file mode 100644 index 0000000..0eea0b9 --- /dev/null +++ b/logs/user_info.log @@ -0,0 +1 @@ +[2026-04-01 02:04:12] Hello, Alice from India. You are 22 years old and categorized as: Adult. diff --git a/run_all.sh b/run_all.sh new file mode 100755 index 0000000..997c623 --- /dev/null +++ b/run_all.sh @@ -0,0 +1,98 @@ +#!/usr/bin/env bash + +set -euo pipefail + +LOG_DIR="logs" +LOG_FILE="$LOG_DIR/app.log" + +mkdir -p "$LOG_DIR" + +timestamp() { + date "+%Y-%m-%d %H:%M:%S" +} + +log_action() { + echo "[$(timestamp)] $1" | tee -a "$LOG_FILE" +} + +run_system_check() { + log_action "Running system_check.sh" + if ./scripts/system_check.sh; then + log_action "system_check.sh completed successfully" + else + log_action "system_check.sh failed" + fi +} + +run_backup() { + read -r -p "Enter directory path to back up: " backup_dir + log_action "Running backup.sh for '$backup_dir'" + if ./scripts/backup.sh "$backup_dir"; then + log_action "backup.sh completed successfully" + else + log_action "backup.sh failed" + fi +} + +run_all() { + log_action "Running all core scripts" + + if ./scripts/user_info.sh; then + log_action "user_info.sh completed successfully" + else + log_action "user_info.sh failed" + fi + + run_system_check + + if ./scripts/file_manager.sh list .; then + log_action "file_manager.sh completed successfully" + else + log_action "file_manager.sh failed" + fi + + read -r -p "Enter directory path for backup in 'Run all': " backup_dir + if ./scripts/backup.sh "$backup_dir"; then + log_action "backup.sh completed successfully" + else + log_action "backup.sh failed" + fi +} + +show_menu() { + echo + echo "===== DevOps Bash Toolkit =====" + echo "1. Run all" + echo "2. System check" + echo "3. Backup" + echo "4. Exit" + echo "===============================" +} + +main() { + while true; do + show_menu + read -r -p "Choose an option [1-4]: " choice + + case "$choice" in + 1) + run_all + ;; + 2) + run_system_check + ;; + 3) + run_backup + ;; + 4) + log_action "Exiting application" + exit 0 + ;; + *) + echo "Invalid choice. Please select 1-4." + ;; + esac + done +} + +main diff --git a/scripts/backup.sh b/scripts/backup.sh new file mode 100755 index 0000000..18a407d --- /dev/null +++ b/scripts/backup.sh @@ -0,0 +1,44 @@ +#!/usr/bin/env bash + +LOG_DIR="logs" +BACKUP_DIR="backups" +LOG_FILE="$LOG_DIR/backup.log" + +mkdir -p "$LOG_DIR" "$BACKUP_DIR" + +timestamp() { + date "+%Y-%m-%d %H:%M:%S" +} + +log_action() { + echo "[$(timestamp)] $1" | tee -a "$LOG_FILE" +} + +source_dir="$1" + +if [[ -z "$source_dir" ]]; then + log_action "Backup failed: No directory provided. Usage: $0 " + exit 1 +fi + +if [[ ! -d "$source_dir" ]]; then + log_action "Backup failed: '$source_dir' is not a valid directory." + exit 1 +fi + +source_base="$(basename "$source_dir")" +archive_name="backup_${source_base}_$(date +%Y%m%d_%H%M%S).tar.gz" +archive_path="$BACKUP_DIR/$archive_name" + +if tar -czf "$archive_path" -C "$(dirname "$source_dir")" "$source_base"; then + log_action "Backup successful: '$source_dir' -> '$archive_path'" +else + log_action "Backup failed while creating archive for '$source_dir'" + exit 1 +fi + +# Keep only the 5 most recent backups. +ls -1t "$BACKUP_DIR"/*.tar.gz 2>/dev/null | awk 'NR>5' | while IFS= read -r old_backup; do + rm -f "$old_backup" + log_action "Removed old backup: '$old_backup'" +done diff --git a/scripts/file_manager.sh b/scripts/file_manager.sh new file mode 100755 index 0000000..11fc316 --- /dev/null +++ b/scripts/file_manager.sh @@ -0,0 +1,119 @@ +#!/usr/bin/env bash + +LOG_DIR="logs" +LOG_FILE="$LOG_DIR/file_manager.log" + +mkdir -p "$LOG_DIR" + +timestamp() { + date "+%Y-%m-%d %H:%M:%S" +} + +log_action() { + echo "[$(timestamp)] $1" >> "$LOG_FILE" +} + +usage() { + echo "Usage: $0 {create|delete|list|rename} [args]" + echo "Examples:" + echo " $0 create file.txt" + echo " $0 delete file.txt" + echo " $0 list ." + echo " $0 rename old.txt new.txt" +} + +command="$1" + +case "$command" in + create) + file="$2" + if [[ -z "$file" ]]; then + echo "Error: Please provide a filename to create." + usage + exit 1 + fi + + if [[ -e "$file" ]]; then + msg="Create failed: '$file' already exists." + echo "$msg" + log_action "$msg" + exit 1 + fi + + touch "$file" + msg="Created file: '$file'" + echo "$msg" + log_action "$msg" + ;; + + delete) + file="$2" + if [[ -z "$file" ]]; then + echo "Error: Please provide a filename to delete." + usage + exit 1 + fi + + if [[ ! -e "$file" ]]; then + msg="Delete failed: '$file' does not exist." + echo "$msg" + log_action "$msg" + exit 1 + fi + + rm -f "$file" + msg="Deleted: '$file'" + echo "$msg" + log_action "$msg" + ;; + + list) + target="${2:-.}" + if [[ ! -d "$target" ]]; then + msg="List failed: '$target' is not a directory." + echo "$msg" + log_action "$msg" + exit 1 + fi + + echo "Listing files in '$target':" + ls -la "$target" + log_action "Listed directory: '$target'" + ;; + + rename) + old_name="$2" + new_name="$3" + + if [[ -z "$old_name" || -z "$new_name" ]]; then + echo "Error: Please provide source and destination names." + usage + exit 1 + fi + + if [[ ! -e "$old_name" ]]; then + msg="Rename failed: '$old_name' does not exist." + echo "$msg" + log_action "$msg" + exit 1 + fi + + if [[ -e "$new_name" ]]; then + msg="Rename failed: '$new_name' already exists." + echo "$msg" + log_action "$msg" + exit 1 + fi + + mv "$old_name" "$new_name" + msg="Renamed '$old_name' to '$new_name'" + echo "$msg" + log_action "$msg" + ;; + + *) + echo "Error: Invalid command." + usage + exit 1 + ;; +esac diff --git a/scripts/logs/user_info.log b/scripts/logs/user_info.log new file mode 100644 index 0000000..5ef463e --- /dev/null +++ b/scripts/logs/user_info.log @@ -0,0 +1,4 @@ +Name: Israel | Age: 27 | Country: Nigeria | Category: Adult + + + diff --git a/scripts/process_monitor.sh b/scripts/process_monitor.sh new file mode 100755 index 0000000..654a50a --- /dev/null +++ b/scripts/process_monitor.sh @@ -0,0 +1,35 @@ +#!/usr/bin/env bash + +LOG_DIR="logs" +LOG_FILE="$LOG_DIR/process_monitor.log" +services=("nginx" "ssh" "docker") + +mkdir -p "$LOG_DIR" + +timestamp() { + date "+%Y-%m-%d %H:%M:%S" +} + +log_action() { + echo "[$(timestamp)] $1" | tee -a "$LOG_FILE" +} + +process_name="$1" + +if [[ -z "$process_name" ]]; then + echo "Usage: $0 " + echo "Supported examples: ${services[*]}" + exit 1 +fi + +if pgrep -x "$process_name" >/dev/null 2>&1 || pgrep -f "$process_name" >/dev/null 2>&1; then + log_action "Process '$process_name' status: Running" + exit 0 +fi + +log_action "Process '$process_name' status: Stopped" + +# Simulate restart for safety in student environments. +log_action "Attempting restart for '$process_name' (simulated)..." +sleep 1 +log_action "Process '$process_name' status: Restarted (simulated)" diff --git a/scripts/system_check.sh b/scripts/system_check.sh new file mode 100755 index 0000000..8672cf6 --- /dev/null +++ b/scripts/system_check.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +echo "Enter your name:" +read name + +echo "Enter your age:" +read age + +echo "Enter your country:" +read country + +if [ -z "$name" ] || [ -z "$age" ] || [ -z "$country" ]; then + echo "Error: All fields are required." + exit 1 +fi \ No newline at end of file diff --git a/scripts/user_info.sh b/scripts/user_info.sh new file mode 100755 index 0000000..73f94bf --- /dev/null +++ b/scripts/user_info.sh @@ -0,0 +1,42 @@ +#!/bin/bash + +# Create logs directory if it doesn't exist +mkdir -p logs + +# Prompt user for input +echo "Enter your name:" +read name + +echo "Enter your age:" +read age + +echo "Enter your country:" +read country + +# Validate input +if [ -z "$name" ] || [ -z "$age" ] || [ -z "$country" ]; then + echo "Error: All fields are required." + exit 1 +fi + +# Validate age is numeric +if ! [[ "$age" =~ ^[0-9]+$ ]]; then + echo "Error: Age must be a number." + exit 1 +fi + +# Determine age category +if [ "$age" -lt 18 ]; then + category="Minor" +elif [ "$age" -le 65 ]; then + category="Adult" +else + category="Senior" +fi + +# Output message +echo "Hello $name from $country!" +echo "You are classified as: $category" + +# Save to log file +echo "Name: $name | Age: $age | Country: $country | Category: $category" >> logs/user_info.log \ No newline at end of file