From 6b0ebc93caa313283901befeaf94ebcc27c356cf Mon Sep 17 00:00:00 2001 From: Yevhen Baranov Date: Mon, 16 Oct 2023 13:09:34 +0000 Subject: [PATCH] Task 1: Strengthen your shell scripting skills on real-world tasks --- tasks/module_4/task-1/.bashrc | 1 + tasks/module_4/task-1/README.md | 13 ++++++++++ tasks/module_4/task-1/count_files.sh | 4 +++ tasks/module_4/task-1/disk_space_monitor.sh | 28 +++++++++++++++++++++ 4 files changed, 46 insertions(+) create mode 100644 tasks/module_4/task-1/.bashrc create mode 100644 tasks/module_4/task-1/README.md create mode 100644 tasks/module_4/task-1/count_files.sh create mode 100644 tasks/module_4/task-1/disk_space_monitor.sh diff --git a/tasks/module_4/task-1/.bashrc b/tasks/module_4/task-1/.bashrc new file mode 100644 index 0000000..400d545 --- /dev/null +++ b/tasks/module_4/task-1/.bashrc @@ -0,0 +1 @@ +source ./disk_space_monitor.sh \ No newline at end of file diff --git a/tasks/module_4/task-1/README.md b/tasks/module_4/task-1/README.md new file mode 100644 index 0000000..7f14baa --- /dev/null +++ b/tasks/module_4/task-1/README.md @@ -0,0 +1,13 @@ +# PR Instructions + +## Task 1 + +In the first task, a name conflict occurred, so you can find a folder named `task-1` (instead of `task_1`) within the `tasks` directory, which contains a submodule `module_4`. + +## Task 2 + +For the second task, you need to update your `.bashrc` file to match the one in this PR and specify the correct path in the `disk_space_monitor.sh` file. + +## Task 3 + +To check the third task, simply run the script named `count_files.sh`. diff --git a/tasks/module_4/task-1/count_files.sh b/tasks/module_4/task-1/count_files.sh new file mode 100644 index 0000000..f3aa81e --- /dev/null +++ b/tasks/module_4/task-1/count_files.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +count=$(find . -type f | wc -l) +echo "Total number of files is $count" \ No newline at end of file diff --git a/tasks/module_4/task-1/disk_space_monitor.sh b/tasks/module_4/task-1/disk_space_monitor.sh new file mode 100644 index 0000000..ea576a1 --- /dev/null +++ b/tasks/module_4/task-1/disk_space_monitor.sh @@ -0,0 +1,28 @@ +if [ -z "$DISK_THRESHOLD" ]; then + read -p "Enter the threshold value for disk space monitoring (default is 10%): " threshold + threshold=${threshold:-10} + export DISK_THRESHOLD="$threshold" +fi + +DEFAULT_PS1="$PS1" +WARNING_ADDED=false +PREVIOUS_USED_PERCENTAGE=0 + +check_disk_space() { + user_home="$HOME" + df_output=$(df -h "$user_home") + used_percentage=$(echo "$df_output" | awk 'NR == 2 {print $5}' | sed 's/%//') + + if [ "$used_percentage" -ge "$DISK_THRESHOLD" ] && [ "$WARNING_ADDED" = false ]; then + PS1="\[\e[31m\][Warning: $used_percentage% space used] \[\e[0m\]$DEFAULT_PS1" + WARNING_ADDED=true + elif [ "$used_percentage" -lt "$DISK_THRESHOLD" ] && [ "$WARNING_ADDED" = true ]; then + PS1="$DEFAULT_PS1" + WARNING_ADDED=false + fi + + PREVIOUS_USED_PERCENTAGE="$used_percentage" +} + +PROMPT_COMMAND="check_disk_space" +PS1="$DEFAULT_PS1" \ No newline at end of file