From 076b52cd7d9409595ff97d16750cad4fbfc0a768 Mon Sep 17 00:00:00 2001 From: Tes Date: Sun, 29 Mar 2026 17:41:21 +0100 Subject: [PATCH 1/5] create devops-bash-toolkit directory and user_info interactive script --- devops-bash-toolkit-assessment/user_info.sh | 35 +++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100755 devops-bash-toolkit-assessment/user_info.sh diff --git a/devops-bash-toolkit-assessment/user_info.sh b/devops-bash-toolkit-assessment/user_info.sh new file mode 100755 index 0000000..97ad9cf --- /dev/null +++ b/devops-bash-toolkit-assessment/user_info.sh @@ -0,0 +1,35 @@ +#!/usr/bin/env bash + +prompt() { + local name=${1:-} + local age + local country=${2:-} + + while true; do + read -p "Enter age: " age + if [[ ! $age =~ ^([1-9][0-9]*|0)$ ]]; then + echo "Invalid age, try again" + else + echo "Valid age: $age" + break + fi + done +if [[ ${age} -lt 18 ]]; then + echo "You are a minor" +elif [[ ${age} -gt 65 ]]; then + echo "You are an adult" +else + echo "You are a senior" +fi + + + + echo "Your name is ${name} and you are from ${country}, the /$/{result of api_call /}" + echo "Country: ${country}" +} + +mkdir -p logs +cd logs || exit +touch "user_info.log" + +prompt "$@" > "user_info.log" \ No newline at end of file From 6bd7aed7b8cb7e701e27143e5e1142b548ce0c94 Mon Sep 17 00:00:00 2001 From: Tes Date: Sun, 29 Mar 2026 17:57:29 +0100 Subject: [PATCH 2/5] chore: move user_info.sh into /scripts directory --- scripts/user_info.sh | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100755 scripts/user_info.sh diff --git a/scripts/user_info.sh b/scripts/user_info.sh new file mode 100755 index 0000000..97ad9cf --- /dev/null +++ b/scripts/user_info.sh @@ -0,0 +1,35 @@ +#!/usr/bin/env bash + +prompt() { + local name=${1:-} + local age + local country=${2:-} + + while true; do + read -p "Enter age: " age + if [[ ! $age =~ ^([1-9][0-9]*|0)$ ]]; then + echo "Invalid age, try again" + else + echo "Valid age: $age" + break + fi + done +if [[ ${age} -lt 18 ]]; then + echo "You are a minor" +elif [[ ${age} -gt 65 ]]; then + echo "You are an adult" +else + echo "You are a senior" +fi + + + + echo "Your name is ${name} and you are from ${country}, the /$/{result of api_call /}" + echo "Country: ${country}" +} + +mkdir -p logs +cd logs || exit +touch "user_info.log" + +prompt "$@" > "user_info.log" \ No newline at end of file From 17ff57904c52b0c65b74f8abeb91f8212f719e93 Mon Sep 17 00:00:00 2001 From: Tes Date: Wed, 1 Apr 2026 23:59:01 +0100 Subject: [PATCH 3/5] implement file manager, sys check scripts and update user_info script --- scripts/file_manager.sh | 84 +++++++++++++++++++++++++++++++++++++++++ scripts/system_check.sh | 18 +++++++++ scripts/user_info.sh | 5 +-- 3 files changed, 104 insertions(+), 3 deletions(-) create mode 100644 scripts/file_manager.sh create mode 100755 scripts/system_check.sh diff --git a/scripts/file_manager.sh b/scripts/file_manager.sh new file mode 100644 index 0000000..e7491f4 --- /dev/null +++ b/scripts/file_manager.sh @@ -0,0 +1,84 @@ +#!/usr/bin/env bash + + +# Function to show usage/help +show_help() { + echo "Usage: $(basename "$0") [command]" + echo "" + echo "Commands:" + echo " create [filename] Create file titled filename with some user-provided extension (e.g .txt)" + echo " delete [filename] Delete specified filename" + echo " rename [filename] Rename file" + echo " list List files in the current directory" + echo " help Show this help message" +} + +create() { + local file_name=${1:?"File name must be specified for creation, exiting...."} + + if [[ -f "$file_name" ]]; then + echo "File already exists" + else + touch -- "$file_name" && echo "File created" + + fi +} +#refactor delete to confirm before deleting a directory +# add error handling for when delete fails +delete() { + + local file_name=${1:?"A file name must be specified for deletion, exiting...."} + + if [[ ! -f "${file_name}" && ! -d "${file_name}" ]]; then + echo "No file or directory named ${file_name} found" >&2 + exit + elif [[ -f "${file_name}" ]]; then + rm -- -f "${file_name}" + + elif [[ -d "${file_name}" ]]; then + rm -- -rf "${file_name}" + + fi +} + +# + +# Function to list files +list() { + ls -lh +} +# add error handling for when delete fails + +rename() { + local old_name=${1:?"Name of existing file cannot be empty"} + local new_name=${2:?"New name of existing file cannot be empty"} + + if [[ -e "${old_name}" ]]; then + mv -- "${old_name}" "${new_name}" + else + printf "Error: file \'%s\' does not exist" "$old_name" + fi +} + +# Parse the first argument as the command +command=$1 +shift # remove first argument so $@ contains remaining args + +case "$command" in + create | cr) + create "$@" + ;; + delete | del) + delete "$@" + ;; + list | l) + list + ;; + rename | rn) + rename "$@" + ;; + *) + show_help + ;; +esac + diff --git a/scripts/system_check.sh b/scripts/system_check.sh new file mode 100755 index 0000000..139c1ec --- /dev/null +++ b/scripts/system_check.sh @@ -0,0 +1,18 @@ +#!/usr/bin/env bash + +sys_info() { +# Disk usage +df -h | awk 'NR > 1 && ($5 + 0) > 80 {print "Warning: $1 space exceeds 80%"}' +# Memory usage +free -m +# CPU load (uptime) +uptime +# Display top 5 memory-consuming processes +ps aux | awk ' NR > 1 && $4 > 0.0 {print $4} ' | sort -nr | head -5 + +# Count total running processes +ps aux | awk ' {count++} END {print count}' + +} + +sys_info > system_report.log \ No newline at end of file diff --git a/scripts/user_info.sh b/scripts/user_info.sh index 97ad9cf..4a4b4aa 100755 --- a/scripts/user_info.sh +++ b/scripts/user_info.sh @@ -27,9 +27,8 @@ fi echo "Your name is ${name} and you are from ${country}, the /$/{result of api_call /}" echo "Country: ${country}" } - -mkdir -p logs -cd logs || exit +[[ -d "logs" ]] || mkdir logs +[[ -d "logs" ]] && cd logs || exit touch "user_info.log" prompt "$@" > "user_info.log" \ No newline at end of file From 92ab1ead03238a74281082df8d28d3cadd4dcf58 Mon Sep 17 00:00:00 2001 From: Tes Date: Fri, 3 Apr 2026 08:22:31 +0100 Subject: [PATCH 4/5] update: update system_check script --- devops-bash-toolkit-assessment/user_info.sh | 35 --------------------- scripts/system_check.sh | 22 ++++++++----- 2 files changed, 15 insertions(+), 42 deletions(-) delete mode 100755 devops-bash-toolkit-assessment/user_info.sh diff --git a/devops-bash-toolkit-assessment/user_info.sh b/devops-bash-toolkit-assessment/user_info.sh deleted file mode 100755 index 97ad9cf..0000000 --- a/devops-bash-toolkit-assessment/user_info.sh +++ /dev/null @@ -1,35 +0,0 @@ -#!/usr/bin/env bash - -prompt() { - local name=${1:-} - local age - local country=${2:-} - - while true; do - read -p "Enter age: " age - if [[ ! $age =~ ^([1-9][0-9]*|0)$ ]]; then - echo "Invalid age, try again" - else - echo "Valid age: $age" - break - fi - done -if [[ ${age} -lt 18 ]]; then - echo "You are a minor" -elif [[ ${age} -gt 65 ]]; then - echo "You are an adult" -else - echo "You are a senior" -fi - - - - echo "Your name is ${name} and you are from ${country}, the /$/{result of api_call /}" - echo "Country: ${country}" -} - -mkdir -p logs -cd logs || exit -touch "user_info.log" - -prompt "$@" > "user_info.log" \ No newline at end of file diff --git a/scripts/system_check.sh b/scripts/system_check.sh index 139c1ec..b1da5f9 100755 --- a/scripts/system_check.sh +++ b/scripts/system_check.sh @@ -2,17 +2,25 @@ sys_info() { # Disk usage -df -h | awk 'NR > 1 && ($5 + 0) > 80 {print "Warning: $1 space exceeds 80%"}' +echo -e "Disk Usage: " +df -h | awk 'NR > 1 && ($5 + 0) > 80 {print "Warning: " $1 " space exceeds 80%\n"}' # Memory usage -free -m +echo -e "RAM memory usage: \n" +printf "%s\n\n" "$(free -m)" # CPU load (uptime) -uptime +echo "System Uptime info:"; uptime + # Display top 5 memory-consuming processes -ps aux | awk ' NR > 1 && $4 > 0.0 {print $4} ' | sort -nr | head -5 +echo -e "\n\nTop 5 memory consuming processes are: " +ps aux | awk ' NR > 1 && $4 > 0.0 {print $4, $11} ' | sort -nr | head -5 # Count total running processes -ps aux | awk ' {count++} END {print count}' +echo -e "\nTotal running processes are: "; ps aux | awk ' {count++} END {print count}' } - -sys_info > system_report.log \ No newline at end of file +log_date=$(date "+%d-%m-%Y-%S") +[[ -d "logs" ]] && sys_info > logs/system_report_"($log_date)".log +[[ ! -d "logs" ]] && mkdir logs && sys_info > logs/system_report_"($log_date)".log +# # explain >&2 +# how to output potential script errors into another file +# how to generate random ids for filename in bash script \ No newline at end of file From efecc8d450a5ecb1fd0f389507057d538df97e0b Mon Sep 17 00:00:00 2001 From: Tes Date: Mon, 6 Apr 2026 21:41:37 +0100 Subject: [PATCH 5/5] create backup.sh script --- scripts/backup.sh | 48 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 scripts/backup.sh diff --git a/scripts/backup.sh b/scripts/backup.sh new file mode 100644 index 0000000..316baa8 --- /dev/null +++ b/scripts/backup.sh @@ -0,0 +1,48 @@ +#!/usr/bin/env bash + +# create regex for directories +# check for characters invalid in directory names +# how to check if sth exists in bash + + +help() { + echo "${0} creates a compressed backup of a user specified directory" + echo "Usage: " + echo -e "${0} backup\n" + # echo "Example usage: ./backup.sh my_pictures pic_backup" + + + echo "${0} help: Displays this help message" +} + + +backup() { + local dir_name + local backup_name + local backup_date=$(date "+%d-%m-%Y_%H%M%S") + + + read -rp "Specify the name of the directory to backup: " dir_name + read -rp "Specify name of the backup (defaults to dir_name if backup_name is not specified) " backup_name + + if [[ ! -d "${dir_name}" ]]; then + echo -e "\nError: specified directory does not exist" >&2 + fi + [[ -z "${backup_name}" ]] && backup_name="${dir_name}_${backup_date}" && echo "backup name not specified: using ${dir_name} instead " >&1 + [[ -d "${dir_name}" ]] && tar -czvf "${backup_name}_${backup_date}.tar.gz" "${dir_name}" + + +} + +# backup "$@" > log + +command="$1" +case "$command" in + "backup") + backup >> backups.log;; + + *) + help;; +esac + +ps aux | awk ' BEGIN {"ps aux" | getline ps print head} $8=="R+" {print $8, $11}' \ No newline at end of file