From db8786a9816f222c5c484b5a3b0b8e7a4c98424c Mon Sep 17 00:00:00 2001 From: Nils Gerstner Date: Wed, 16 Nov 2022 02:03:03 +0100 Subject: [PATCH] Add templates. Add slack script. Add showmyip --- README.md | 17 ++++- bash/sent_to_slack.sh | 15 ++++ bash/showmyip.sh | 4 ++ bash/templates/template.sh | 97 ++++++++++++++++++++++++++ bash/templates/template_multisystem.sh | 44 ++++++++++++ 5 files changed, 175 insertions(+), 2 deletions(-) create mode 100755 bash/sent_to_slack.sh create mode 100755 bash/showmyip.sh create mode 100755 bash/templates/template.sh create mode 100755 bash/templates/template_multisystem.sh diff --git a/README.md b/README.md index 6fb0fe8..295e4f9 100644 --- a/README.md +++ b/README.md @@ -5,10 +5,23 @@ Collection of scripts and functions to make my developer life easier. **bash/logger/** *Enrich command output for logging purposes* +**bash/q_completion.sh** +*Add TAB completion to [q](https://github.com/ibm-messaging/mq-q-qload) for manipulating IBM MQ messages.* + +**bash/sent_to_slack.sh** +*Send messages to slack* + +**bash/showmyip.sh** +*Show my own external ip address* + **bash/sshconnect.sh** *TUI to display a list of servers with description.* -**bash/q_completion.sh** -*Add TAB completion to [q](https://github.com/ibm-messaging/mq-q-qload) for manipulating IBM MQ messages.* +**bash/templates/template.sh** +*Advanced template for bash scripts* + +**bash/templates/template_multisystem.sh** +*Template for multisystem aware script* + diff --git a/bash/sent_to_slack.sh b/bash/sent_to_slack.sh new file mode 100755 index 0000000..b8d67e5 --- /dev/null +++ b/bash/sent_to_slack.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env bash +# Autor: Nils Gerstner + +URL="https://hooks.slack.com/services/TOKENKEY" +username="$1" +message=$2 +ICON=":ghost:" + +payload="{ + \"username\": \"${username}\", + \"text\": \"$message\", + \"icon_emoji\": \"${ICON}\" +}" + +curl -X POST -H "Content-Type: application/json" -d "${payload}" $URL diff --git a/bash/showmyip.sh b/bash/showmyip.sh new file mode 100755 index 0000000..a0f16f0 --- /dev/null +++ b/bash/showmyip.sh @@ -0,0 +1,4 @@ +#!/bin/bash +myip="$(dig +short myip.opendns.com @resolver1.opendns.com)" +#echo "My public IP address is: ${myip}" +echo "${myip}" diff --git a/bash/templates/template.sh b/bash/templates/template.sh new file mode 100755 index 0000000..72e3921 --- /dev/null +++ b/bash/templates/template.sh @@ -0,0 +1,97 @@ +#!/bin/bash +# Author: Nils Gerstner +# Last revision: 15 nov 2022 18:00 +# Version: v0.1 +# Description: + +#=== VARIABLES ================================================================# + +declare -r SCRIPT_NAME=$(basename "$BASH_SOURCE" .sh) +declare -r HELP_MSG="Usage: $SCRIPT_NAME [OPTION]... [ARG]... + -h display this help and exit +" + +#=== FUNCTIONS ================================================================# + +## Print colored messages +_cmsg() { + case "$1" in + black) COLOR="\033[0;30m" ;; + white) COLOR="\033[1;37m" ;; + grey) COLOR="\033[1;30m" ;; + lgrey) COLOR="\033[0;37m" ;; + red) COLOR="\033[0;31m" ;; + lred) COLOR="\033[1;31m" ;; + green) COLOR="\033[0;32m" ;; + lgreen) COLOR="\033[1;32m" ;; + brown) COLOR="\033[0;33m" ;; + yelow) COLOR="\033[1;33m" ;; + blue) COLOR="\033[0;34m" ;; + lblue) COLOR="\033[1;34m" ;; + purple) COLOR="\033[0;35m" ;; + lpurple) COLOR="\033[1;35m" ;; + cyan) COLOR="\033[0;36m" ;; + lcyan) COLOR="\033[1;36m" ;; + nocolor) COLOR="\033[0m" ;; + esac + printf "${COLOR}${2}\33[0m\n" +} + +## exit the shell(default status code: 1) after printing the message to stderr +_bail() { + echo -ne "$1" >&2 + exit ${2-1} +} + +## print the usage and exit the shell(default status code: 2) +_usage() { + local status=2 + if [[ "$1" =~ ^[0-9]+$ ]]; then + status=$1 + shift + fi + _bail "${1}$HELP_MSG" $status +} + +_failure() { + local lineno=$1 + local msg=$2 + echo "Failed at $lineno: $msg" +} + +#=== SET ENVIRONMENT AND EXIT CONDITIONS ======================================# + +# -e Exit immediately on error +# -E ERR trap is inherited by shell functions +set -eE -o functrace + +trap '_failure ${LINENO} "$BASH_COMMAND"' ERR + +#=== HANDLE ARGUMENTS =========================================================# + +while getopts ":hie:" opt; do + case $opt in + i) + # Do something + #(for ex. execute a function or set a switch variable) + ;; + e) + # Option with argument + e_argument=${OPTARG} + ;; + h) + _usage 0 + ;; + \?) + _usage "Invalid option: -$OPTARG \n" + ;; + esac +done + +shift $OPTIND +[[ "$#" -lt 1 ]] && usage "Too few arguments\n" + +#=== MAIN CODE BELOW ==========================================================# + +echo "Start!" + diff --git a/bash/templates/template_multisystem.sh b/bash/templates/template_multisystem.sh new file mode 100755 index 0000000..2316988 --- /dev/null +++ b/bash/templates/template_multisystem.sh @@ -0,0 +1,44 @@ +#! /bin/bash +# Author: Nils Gerstner +# Last revision: 30. December 2018 +# Version: v1 +# Description: + +function _linux() { + # Put your Linux related code here +} + +function _mac() { + # Put your Mac related code here +} + +function _cmsg { + # Print colored messages + # _cmsg color "message" + case "$1" in + black) COLOR="\033[0;30m" ;; + white) COLOR="\033[1;37m" ;; + grey) COLOR="\033[1;30m" ;; + lgrey) COLOR="\033[0;37m" ;; + red) COLOR="\033[0;31m" ;; + lred) COLOR="\033[1;31m" ;; + green) COLOR="\033[0;32m" ;; + lgreen) COLOR="\033[1;32m" ;; + brown) COLOR="\033[0;33m" ;; + yelow) COLOR="\033[1;33m" ;; + blue) COLOR="\033[0;34m" ;; + lblue) COLOR="\033[1;34m" ;; + purple) COLOR="\033[0;35m" ;; + lpurple) COLOR="\033[1;35m" ;; + cyan) COLOR="\033[0;36m" ;; + lcyan) COLOR="\033[1;36m" ;; + nocolor) COLOR="\033[0m" ;; + esac + printf "${COLOR}${2}\33[0m\n" +} + +case "$(uname -s)" in + Linux*) _linux ;; + Darwin*) _mac ;; + *) _cmsg -red "Unknown System\nDo nothing!" +esac