Differences

This shows you the differences between two versions of the page.

Link to this comparison view

tech:linux:bash_loops [2019/11/14 06:28] (current)
Line 1: Line 1:
 +====== bash loops ======
 +===== while counter =====
 +<code bash>
 +#!/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
 +</​code>​
 +
 +===== while file input =====
 +<code bash>
 +#!/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
 +</​code>​
 +
 +
 +===== for loop =====
 +Check all the fail2ban apache configurations for all the apache log files
 +<code bash>
 +#!/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
 +</​code>​
  

QR Code
QR Code tech:linux:bash_loops (generated for current page)