Code-Schnipsel

Unsortierte Sammlung von Codeschnipseln, die ich für erinnerns- oder wiederverwendungswert halte. Reminder: Ich bin kein Programmierer, use at your own risk and don’t judge me. :)

Navigation derzeit am besten via STRG+ALT+t oder den Klick auf das TOC-Symbol oben links.

[bash] Script-Skelett

#!/usr/bin/env bash
########################################################################
#
# Script:    Short description what this script does
# Author:    Folker "ph0lk3r" Schmidt
# Contact:   <info@ichbinein.org>
#
# Changelog (newest first):
#   - 2024-10-04: updated draft
#   - 2024-10-03: first draft
#
########################################################################

# Set error handling options
set -o errexit   # abort on nonzero exitstatus
set -o nounset   # abort on unbound variable
set -o pipefail  # don't hide errors within pipes

# Define script version
readonly version="0.0.1"

# Define some constants for later use
script_path="$(readlink -f "${0}")"
script_dir="$(dirname "${script_path}")"
script_name="$(basename "${script_path}")"
execution_time="$(date +%y-%m-%d_%H-%M-%S)"
logfile="${execution_time}_${script_name}.log"
readonly script_path
readonly script_dir
readonly script_name
readonly execution_time
readonly logfile

# Define some global variables
quiet=0

# Display usage information
function display_help {
  echo "
  usage: ${script_name} [options]
  -a            optional    Explanation for parameter -a
  -b            optional    Explanation for parameter -b
  -c arg        optional    Explanation for parameter -c with argument
  -h            optional    Print this help message
  -q            optional    Do not print logged messages"
}

# Log to a file
function log_to_file {
  current_timestamp="$(date)"
  message="${1}"
  if [[ $quiet -eq 1 ]]
  then
    echo "${current_timestamp}: ${message}" >> "${logfile}"
  else
    echo "${current_timestamp}: ${message}" | tee -a "${logfile}"
  fi
}

# Parse command line parameters
function parse_arguments {
  while getopts ':abc:hq' opt; do
    case "$opt" in
      a)
        echo "Processing option 'a'"
        ;;

      b)
        echo "Processing option 'b'"
        ;;

      c)
        arg="$OPTARG"
        echo "Processing option 'c' with '${arg}' argument"
        ;;

      h)
        display_help
        exit 0
        ;;

      q)
        quiet=1
        ;;
      :)
        echo -e "option requires an argument.\nUsage: ${script_name} [-a] [-b] [-c arg] [-h] [-q]"
        exit 2
        ;;

      ?)
        echo -e "Invalid command option.\nUsage: ${script_name} [-a] [-b] [-c arg] [-h] [-q]"
        exit 3
        ;;
    esac
  done
  shift "$(( OPTIND - 1 ))"
}

# Main code
function main {
  log "Script ${script_name} (version ${version}) called from directory ${script_dir}."
  parse_arguments "${@}"
}

# Just calling the main function after all functions are defined
main "${@}"

[bash] Text zentrieren

function display_center {
  columns="50"
  message="${1}"
  printf "%*s\n" $(( (${#message} + columns) / 2)) "${message}"
}