#!/bin/bash #!/bin/bash echo_rgb() { # Echo a colored string to the terminal based on rgb values # # Positional Arguments: # # message # - The message to be printed to stdout # red # - The red value from 0 to 255 # green # - The green value from 0 to 255 # blue # - The blue value from 0 to 255 # # Usage: # echo_rgb "Yep" 10 8 30 # # POSIX Compliant: # N/A # local red local green local blue local input input="${1}" red="${2}" green="${3}" blue="${4}" printf "\e[0;38;2;%s;%s;%sm%s\e[m\n" "${red}" "${green}" "${blue}" "${input}" } important() { echo_rgb "${1}" 135 195 255 } log() { # Print a message and send it to stdout or stderr depending upon log level, also configurable with debug etc. # # Arguments: # level # - The log level, defined within a case check in this function # message # - The info message # line_number # - The line number of the calling function (${LINNO}) # # Usage: # log "info" "Could not find that directory" # # POSIX Compliant: # Yes # # Set debug status depending if a global debug variable has been set to either 1 or 0 local debug if [ ${DEBUG} ]; then debug=${DEBUG} else debug=0 fi local FORMAT FORMAT="[$(echo_rgb "$(date +%Y-%m-%dT%H:%M:%S)" 180 140 255)]" # Convert the level to uppercase local level level=$(echo "${1}" | tr '[:lower:]' '[:upper:]') local message message="${2}" case "${level}" in INFO) # Output all info log levels to stdout printf "${FORMAT}[$(echo_rgb "INFO" 0 140 255)] %s\n" "${message}" >&1 return 0 ;; WARN | WARNING) # Output all warning log levels to stdout printf "${FORMAT}[$(echo_rgb "WARNING" 255 255 0)] %s\n" "${message}" >&1 return 0 ;; DEBUG) # Output all debug log levels to stdout if [ "${DEBUG}" ]; then printf "${FORMAT}[$(echo_rgb "DEBUG" 0 160 110)] %s\n" "${message}" >&1 fi return 0 ;; ERROR) # Output all error log levels to stderr printf "${FORMAT}[$(echo_rgb "ERROR" 255 0 0)] %s\n" "${message}" >&2 return 0 ;; # Further log levels can be added by extending this switch statement with more comparisons *) # Default case, no matches # Returns non-zero code as an improper log option was passed, this helps with using `set -e` printf "${FORMAT}[ERROR] %s\n" "Invalid log level passed, received level \"${level}\" with message \"${message}\"" >&2 return 1 ;; esac } get_available_disks() { local available_disks available_disks=() local disk_line while read -r; do # tr is used to change multiple whitespaces into a single space for processing by cut disk_line="$(echo "${REPLY}" | tr -s " " | cut -d " " -f6)" if [[ "${disk_line}" = "disk" ]]; then # Add the found disk to our array available_disks+=("$(echo "${REPLY}" | cut -d " " -f1)") fi done <<< "$(lsblk)" local ret_val # Build the array into a string, yes this is less efficient, but easier to modify in the future if something changes for disk in "${available_disks[@]}"; do ret_val="${ret_val} ${disk}" done echo "${ret_val}" } install() { local available_disks log "info" "Querying system for available disks..." available_disks="$(get_available_disks)" log "info" "Available disks: " for disk in ${available_disks}; do echo "- $(important "${disk}")" done } main() { install } main "$@"