Service status monitor

Using the below output, filter and return only services that are not OK.

cache: OK
DB: NOK

Payment: OK
twitter:  down
aws: degraded service
#!/bin/bash

# save output to file for parsing
OUTPUT=$(curl -qs $1 > tmpoutput)

# iterate over lines in file
while read line; do
  if [[ ! -z "$line" ]]; then
    echo "$line" |  grep -Ev " OK$"  # return only lines that do not end in " OK"
  fi
done <tmpoutput

IP address logs

Given a log file that contains system details, including IP addresses.

Filter the log and return only the address 192.168.21.2 .

grep 192.168.21.2 log_file

Filter the log and return all IP addresses (only IP addresses).

grep -E -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' log_file

Fizzbuzz numbers

Print every number from 1 to 100

echo {1..100}

# OR

for number in {1..100}; do
  echo $number
done

Print every number from 1 to 100 that is not divisible by 2

for number in {1..100}; do
    if [[ "(( $number % 2 ))" -ne 0 ]]; then  # check if not modulo 2 (not divisible by 2 keeping whole number)
        echo $number
    fi
done

Print every number from 1 to 100 that is divisible by 5

for number in {1..100}; do
    if [[ "(( $number % 5 ))" -eq 0 ]]; then  # check if modulo 5 (divisible by 5 keeping whole number)
        echo $number
    fi
done

Print every number from 1 to 100 that is both not divisible by 2 and is divisible by 5

for number in {1..100}; do
    if [[ "(( $number % 2 ))" -ne 0 ]] && [[ "(( $number % 5 ))" -eq 0 ]]; then
        echo $number
    fi
done