From 0d07d33c5931fc4fa92643136d452193e38da7c9 Mon Sep 17 00:00:00 2001 From: cloud0x42 Date: Tue, 14 Oct 2025 21:38:29 +0200 Subject: [PATCH] Add bash scripts --- README.md | 4 ++++ scripts/backup.sh | 31 +++++++++++++++++++++++++++++++ scripts/countdown.sh | 14 ++++++++++++++ scripts/fruits.sh | 18 ++++++++++++++++++ scripts/hello.sh | 7 +++++++ scripts/path.sh | 14 ++++++++++++++ 6 files changed, 88 insertions(+) create mode 100644 README.md create mode 100644 scripts/backup.sh create mode 100644 scripts/countdown.sh create mode 100644 scripts/fruits.sh create mode 100644 scripts/hello.sh create mode 100644 scripts/path.sh diff --git a/README.md b/README.md new file mode 100644 index 0000000..98e512d --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +# Git demo + + +This project contains 5 bash scripts, and 3 branches we can use for testing. diff --git a/scripts/backup.sh b/scripts/backup.sh new file mode 100644 index 0000000..790ba97 --- /dev/null +++ b/scripts/backup.sh @@ -0,0 +1,31 @@ +#!/bin/bash + +set -e + +ARCHIVE="/home/ubuntu/backup.tar.gz" +LOG_DIRECTORY="/home/ubuntu/logs" + +if ! [[ -d "$LOG_DIRECTORY" ]]; then + echo "Log directory not found: $LOG_DIRECTORY" + exit 1 +fi + + +log_count=0 + +for file in "$LOG_DIRECTORY"/*.log; do + if [[ -f "$file" ]]; then + log_count=$(( log_count + 1 )) + fi +done + + +echo "Found $log_count log files." + + +if (( log_count > 0 )); then + tar czf "$ARCHIVE" "$LOG_DIRECTORY"/*.log + echo "Successfully created archive: $ARCHIVE" +else + echo "No files found, skipping backup." +fi diff --git a/scripts/countdown.sh b/scripts/countdown.sh new file mode 100644 index 0000000..6b8a833 --- /dev/null +++ b/scripts/countdown.sh @@ -0,0 +1,14 @@ +#!/bin/bash + +set -e + +counter=${1:-10} + + +while (( counter > 0 )); do + echo "$counter" + sleep 1 + (( counter-- )) +done + +echo "Tada..." \ No newline at end of file diff --git a/scripts/fruits.sh b/scripts/fruits.sh new file mode 100644 index 0000000..13f3d87 --- /dev/null +++ b/scripts/fruits.sh @@ -0,0 +1,18 @@ +#!/bin/bash + +set -e + +liked_count=0 +total_count=0 + + +for fruit in banana apple strawberry; do + read -p "Do you like $fruit (y/n): " answer + if [[ "$answer" == y* ]]; then + liked_count=$(( liked_count + 1 )) + fi + + total_count=$(( total_count + 1 )) +done + +echo "Liked $liked_count fruits out of $total_count" diff --git a/scripts/hello.sh b/scripts/hello.sh new file mode 100644 index 0000000..ca7facc --- /dev/null +++ b/scripts/hello.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +set -e + +read -p "Your name: " name +today=$(date "+%d-%m-%Y") +echo "Hello $name. Today is $today." diff --git a/scripts/path.sh b/scripts/path.sh new file mode 100644 index 0000000..150e245 --- /dev/null +++ b/scripts/path.sh @@ -0,0 +1,14 @@ +#!/bin/bash + +set -e + +read -rp "Path: " input_path + + +if [[ -f "$input_path" ]]; then + echo "It's a file." +elif [[ -d "$input_path" ]]; then + echo "It's a directory." +else + echo "It's neither." +fi \ No newline at end of file