#!/usr/bin/env bash

SEP="########################################################################"
MY_VAR_1=
MY_VAR_2="foo"

# -n: True if the length of string is non-zero.
if [[ -n "$MY_VAR_1" ]]; then
    echo "MY_VAR_1 is set"
elif [[ -n "$MY_VAR_2" ]]; then
    echo "MY_VAR_2 is set"
else
    echo "No vars are set"
fi

printf "\n%s\n" "$SEP"

# -z: True if the length of string is zero.
if [[ -z "$MY_VAR_1" ]]; then
    echo "MY_VAR_1 is an empty string"
elif [[ -z "$MY_VAR_2" ]]; then
    echo "MY_VAR_2 is an empty string"
else
    echo "Neither var is an empty string"
fi

printf "\n%s\n" "$SEP"

# -v: True if the shell variable varname is set (has been assigned a value).
if [ -v "$MY_VAR_3" ]; then
    echo "MY_VAR_3 is set"
else
    echo "MY_VAR_3 is not set"
fi

printf "\n%s\n" "$SEP"

# -f: True if file exists and is a regular file.
FILE_NAME="./examples/01-output.sh"
if [[ -f "$FILE_NAME" ]]; then
    echo "$FILE_NAME exists"
fi

printf "\n%s\n" "$SEP"

# !: negates result
# -d: True if file exists and is a regular file.
DIRECTORY_NAME="./examples"
if [[ ! -d "$DIRECTORY_NAME/fake-dir" ]]; then
    echo "$DIRECTORY_NAME/fake-dir doesn't exist"
fi

printf "\n%s\n" "$SEP"

# Count lines with wc, trim spaces with tr
LINE_COUNT=$(wc -l < "$FILE_NAME" | tr -d " ")
# -gt: True if left argument is greater than right argument
if [[ $LINE_COUNT -gt 0 ]]; then
    echo "$FILE_NAME has ${LINE_COUNT} lines"
else
    echo "$FILE_NAME is empty"
fi

printf "\n%s\n" "$SEP"

# =: Check equality of strings
if [[ "$MY_VAR_1" = "$MY_VAR_2" ]]; then
    echo "Vars 1 & 2 are the same"
else
    echo "Vars 1 & 2 are different"
fi

printf "\n%s\n" "$SEP"
