29/07/20 Notes
Bash Scripting
#!/bin/bash

# Debugging
#set -x

# For loop example
for_loop_function () {
  directory=$1
  dir_contents=($(ls ${directory}))
  for file in "${dir_contents[@]}"
  do
    echo "Deleting file ${file}"
    rm "${directory}/${file}"
  done
}

# Using Exit Codes
exit_code_function () {
  echo "Enter the absolute name of the file to create."
  read file
  touch $file
  if [ $? -eq 0 ]; then
    echo "${file} created."
    ls -l $file
  else
    echo "Unable to create ${file}. Check fullpath."
  fi  
}

# While loop example
while_loop_function () {
  value=$1
  while [ $value -le 5 ]
  do
    echo "Hello World ${value} times"
    value=$(( $value + 1 ))
  done
}

# Conditional Example
conditional_function () {
  action=$1
  username=$2
  if [ $action == "create" ]; then
    useradd $username -m
  elif [ $action == "delete" ]; then
    userdel $username -r
  else
    echo "You did not enter create or delete for your first parameter"
  fi
}  

# Function that checks for required arguments
wget_function () {
  if [ $# -ne 2 ]; then
    echo 'Welcome to the website download script.'
    echo 'This script accept two arguments.'
    echo 'The first is the full path of the local download location.'
    echo 'The second is the URL of the website to download.'
    exit 0
  else
    echo "Creating directory ${1}"
    time_stamp=$(date +%Y-%m-%d-%T)
    mkdir -p "${1}/${time_stamp}"
    cd "${1}/${time_stamp}"
    echo "Downloading website ${2}"
    wget --no-clobber --convert-links --random-wait -r -p -E -e robots=off -U mozilla $2
  fi
}

# and/or function
and_or_function () {
  if [ $1 -gt 0 -a $1 -lt 2 ]; then
    echo "You entered 1."
  elif [ $1 -ge 2 -o $1 -gt 3 ]; then
    echo "You entered a number greater than 2 and 3."
  elif
    [ $1 -le 0 ]; then
    echo "You entered a number less than or equal to 0."
  else
    echo "You did not enter an integer." 
  fi
}

# Nested if
file_exist_function () {
  if [ $# -ne 1 ]; then
    echo "This script expects 1 argument, that of a full filepath."
    exit 0
  fi  
  if [ ! -f $1 ]; then
    echo "$1 does not exist."
    echo "Do you want to create ${1}? Enter 'y' to create, any other character to escape."
    read answer
    if [ $answer == 'y' ]; then
      touch $1
    else
      exit 0
    fi
  elif test -f $1; then
    echo "File exists. Enter 'y' to overwrite it, any other character to skip."
    read answer
    if [ $answer == 'y' ]; then
      touch $1
    else
      exit 0
    fi
  fi
}

# A tool
case_function () {
  echo "Welcome to this widget"
  while true; do
  echo "Enter 1 for date"
  echo "Enter 2 for hostname"
  echo "Enter 3 for disk"
  echo "Enter 4 for process"
  echo "Enter 5 for user creation"
  echo "Enter 6 for user deletion"
  echo "Enter exit to end program"
    read selection
    case $selection in
    1)  date
        ;;
    2)  hostname
        ;;
    3)  df -h
        ;;
    4)  ps -aux
        ;;
    5)  echo "Enter username to create."
        read username
        sudo -s useradd $username -m
        home="file /home/${username}"
        echo "${username} created."
        $home
        ;;
    6)  echo "Enter username to delete."
        read username
        sudo -s userdel $username -r
        echo "${username} deleted."
        ;; 
    exit) break
        ;;
    *)  echo "Invalid response"
        ;;
  esac
done
}

# Function list
# Uncomment a function and call the bash script to invoke it.
#for_loop_function $1
#exit_code_function
#while_loop_function $1
#conditional_function $1 $2
#wget_function $1 $2
#and_or_function $1
#file_exist_function $1
#case_function