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 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..b1da5f9 --- /dev/null +++ b/scripts/system_check.sh @@ -0,0 +1,26 @@ +#!/usr/bin/env bash + +sys_info() { +# Disk usage +echo -e "Disk Usage: " +df -h | awk 'NR > 1 && ($5 + 0) > 80 {print "Warning: " $1 " space exceeds 80%\n"}' +# Memory usage +echo -e "RAM memory usage: \n" +printf "%s\n\n" "$(free -m)" +# CPU load (uptime) +echo "System Uptime info:"; uptime + +# Display top 5 memory-consuming processes +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 +echo -e "\nTotal running processes are: "; ps aux | awk ' {count++} END {print count}' + +} +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 diff --git a/scripts/user_info.sh b/scripts/user_info.sh new file mode 100755 index 0000000..4a4b4aa --- /dev/null +++ b/scripts/user_info.sh @@ -0,0 +1,34 @@ +#!/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}" +} +[[ -d "logs" ]] || mkdir logs +[[ -d "logs" ]] && cd logs || exit +touch "user_info.log" + +prompt "$@" > "user_info.log" \ No newline at end of file