Differences
This shows you the differences between two versions of the page.
| — | tech:linux:bash_loops [2020/09/29 11:27] (current) – created - external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== bash loops ====== | ||
| + | ===== while counter ===== | ||
| + | <code bash> | ||
| + | #!/bin/bash | ||
| + | # | ||
| + | SLEEPTIME=5 | ||
| + | MAXCOUNT=5 | ||
| + | URL=" | ||
| + | # | ||
| + | COUNTER=0 | ||
| + | while [ $COUNTER -lt $MAXCOUNT ]; do | ||
| + | let COUNTER=COUNTER+1 | ||
| + | echo " | ||
| + | " | ||
| + | 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 | ||
| + | <code bash> | ||
| + | #!/bin/bash | ||
| + | SLEEPTIME=10 | ||
| + | RETCODE=1 | ||
| + | COUNTER=0 | ||
| + | cd / | ||
| + | while [ $RETCODE -gt 0 ]; do | ||
| + | let COUNTER=COUNTER+1 | ||
| + | ./launcher bootstrap app | ||
| + | RETCODE=$? | ||
| + | echo "( $COUNTER ) -----------------------------------------------------------------" | ||
| + | sleep $SLEEPTIME | ||
| + | done | ||
| + | echo " | ||
| + | </ | ||
| + | |||
| + | ===== while file input ===== | ||
| + | <code bash> | ||
| + | #!/bin/bash | ||
| + | # | ||
| + | HOSTFILE=" | ||
| + | # tail to remove header | ||
| + | cat $HOSTFILE | tail -n +2 | while read ServerName site os platform pvlan vlan ip subnet mask gateway nat | ||
| + | do | ||
| + | echo " | ||
| + | ping -c 1 $ip | ||
| + | if [ $? -gt 0 ]; then | ||
| + | echo " | ||
| + | fi | ||
| + | done | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== for loop ===== | ||
| + | Check all the fail2ban apache configurations for all the apache log files | ||
| + | <code bash> | ||
| + | #!/bin/bash | ||
| + | # | ||
| + | for configfile in $(ls / | ||
| + | for logfile in $(find / | ||
| + | echo "Log: $logfile --- Config: $configfile" | ||
| + | / | ||
| + | done | ||
| + | done | ||
| + | </ | ||