Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

In this article you will learn on how to create script which allows to create alerts when some directories have been deleted or some have been created after running script. This script has algorithm which is checking all directories 100 times.

Code Block
languagebash
#!/bin/bash


# Compare two strings
string1="jira"
string2="09"
string3="10"

# array with created jira servers
jiras=()

# declare inital start here
initial_start=true
scanning=true
total_jiras=

# Specify the file name

# Check if the file already exists
if [ ! -e "alerts.txt" ]; then
          # Create the file only if it doesn't exist
            echo "Hello, this is some text." > "alerts.txt"
            echo "File created successfully."
      else
            echo "File already exists. Not overwriting."
fi



while [ scanning ]; do

# this will return all jira dir where script has been executed
# it should be executed inside ./docker : sudo bash ./docker/mswitala/crobjobs.sh
dires=$(ls | grep "jira*")

# Convert the space-separated list into an array
IFS=$'\n' read -r -d '' -a sorted_array <<< "$dires"

# test
echo " first === scanning loop === "


for item in "${sorted_array[@]}"; do

counter2=10
counter=1
max=9
max2=99

        #echo " just checking items in array : $item"
        #echo "count number : $counter"

#checking dirs from 01 to 09
while [ "$counter" -le "$max" ]; do

  number=0
  string=$string1$number$counter

 # echo " test checking string >  $string"

        if [ "$item" == "$string" ]; then
        #echo " adding $item because it is equal to $string"
        jiras+=("$item")
        break
        #else
        #echo "The string $item is equal to $string"
        #echo " "
        fi
        ((counter++))
                                                                                                                           45,1          31%

done

# chceking dirs from 10 to 99
while [ "$counter2" -le "$max2" ]; do

  string=$string1$counter2

  #echo " test checking string >  $string"

        if [ "$item" == "$string" ]; then
        #   echo " adding $item because it is equal to $string"
       jiras+=("$item")
        break
        #else
        #      echo "The string $item ise not equal to $string"
        fi
        ((counter2++))
done


done

# Check the number of items in the array


# check now if all jira dirs have been collected if new have been created
if $initial_start; then

        #test
        # save just ONCE how many jira dir there is now
        total_jiras="${#jiras[@]}"
        initial_start=false
        echo " total jiras as for now is equal to ===== > $total_jiras "


fi

current_jiras=${#jiras[@]}

# create alerts here to notifty user
if [ "$current_jiras" -gt "$total_jiras" ]; then

        current_date=$(date)
        echo "ALERT there has been created new jira server."
        echo "ALERT on $current_date there has been created new jira server. " >> "alerts.txt"


        break
fi

if [ "$current_jiras" -lt "$total_jiras" ]; then


        echo "ALERT some jira server has been deleted. "

        current_date=$(date)
        echo "ALERT some jira server has been deleted."
        echo  "ALERT on $current_date ALERT some jira server has been deleted. " >> "alerts.txt"


        break

fi


unset jiras
unset dires

done