# bash scripting

# loops

looping through directory listing:

for i in $( ls ); do
    echo item: $i
 done

loop through a file (dirs.txt) with a list of directories and copy

#!/bin/bash
while read p; do
  echo $p
  cp PRODUCT_INFO.md $p
done < dirs.txt

range:

for i in {0..40}; do wget http://10.10.10.245/data/${i}; done

inifinite loop:

while true; do nc -w0 -v 10.10.10.10 514 <<< "Hello There"; sleep 2; done

# If statements

if [ $# -ne 2 ]
then
    echo "Error in $0 - Invalid Argument Count"
    echo "Syntax: $0 <push|pull> <Product Name>"
    exit
fi

# functions

function simple_function {
rsync -zav 10.10.10.10:/var/www/simple/ /var/www/simple
}

# case

if [ "$1" = "foo" ]; then
case "$2" in

bar)  echo "fooing bar $2"
    foo_bar
    ;;

*) echo "Command Not recognized"
   ;;
esac

fi

# ping scan a network

#!/bin/bash

is_alive_ping()
{
  ping -c 1 $1 > /dev/null
  [ $? -eq 0 ] && echo Node with IP: $i is up.
}

for i in 10.5.5.{1..255} 
do
is_alive_ping $i & disown
done