Table of Contents

bash loops

while counter

#!/bin/bash
#
SLEEPTIME=5
MAXCOUNT=5
URL="https://www.example.com/xys"
#
COUNTER=0
while [  $COUNTER -lt $MAXCOUNT ]; do
    let COUNTER=COUNTER+1
    echo "Iteration: $COUNTER
"
    curl -k $URL
    echo "
Status: $?
"
    # Don't execute for last iteration
    if [  $COUNTER -lt $MAXCOUNT ]; then
      sleep $SLEEPTIME
    fi
done

Repeat a process until successful

#!/bin/bash
SLEEPTIME=10
RETCODE=1
COUNTER=0
cd /opt/discourse
while [ $RETCODE -gt 0 ]; do
    let COUNTER=COUNTER+1
    ./launcher bootstrap app
    RETCODE=$?
    echo "( $COUNTER ) -----------------------------------------------------------------"
    sleep $SLEEPTIME
done
echo "Installed"|mailx -s Discourse system@example.org

while file input

#!/bin/bash
#
HOSTFILE="hostdetails.txt"
# tail to remove header
cat $HOSTFILE | tail -n +2 | while read ServerName site os platform pvlan vlan ip subnet mask gateway nat
do
  echo "=========== $ServerName - $ip ==========="
  ping -c 1 $ip
  if [ $? -gt 0 ]; then
    echo "---------------------------------------------------------->> Error"
  fi
done

for loop

Check all the fail2ban apache configurations for all the apache log files

#!/bin/bash
#
for configfile in $(ls /etc/fail2ban/filter.d/apache-*); do
  for logfile in $(find /var/log/apache2 -name "*.log"); do
    echo "Log: $logfile --- Config: $configfile"
    /usr/bin/fail2ban-regex $logfile $configfile | grep matched
  done
done