Skip to content

Instantly share code, notes, and snippets.

@nvogel
Last active May 10, 2021 13:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nvogel/29793468dcff3626c3009308c410f0d6 to your computer and use it in GitHub Desktop.
Save nvogel/29793468dcff3626c3009308c410f0d6 to your computer and use it in GitHub Desktop.
bash_tips
#!/usr/bin/env bash
# https://github.com/kward/shflags
# https://odb.github.io/shml/getting-started/#Install
# Path to script directory
__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck disable=SC1090
source "${__dir}/lib/shml.sh"
# shellcheck disable=SC1090
source "${__dir}/lib/shflags.sh"
function usage()
{
echo "......."
echo "$0 [-v VERSION]"
}
function info()
{
local MSG
MSG="[$(date +%Y-%m-%d\ %H:%M:%S)] $*"
# shellcheck disable=SC2046
echo $(fgcolor blue "$MSG") $(bgcolor end)
}
function error()
{
local MSG
MSG="[$(date +%Y-%m-%d\ %H:%M:%S)] $*"
# shellcheck disable=SC2046
echo $(fgcolor red "$MSG") $(bgcolor end)
}
yell() { echo "Error with the command $0: $*" >&2; }
die() { yell "$*"; exit 111; }
try() { "$@" || die "cannot $*"; }
# Action
DEFINE_boolean 'create' false 'Create ......' 'c'
# Manage help message without argument
if [ $# == 0 ] ; then
flags_help
exit 1;
fi
# Parse arguments
FLAGS "$@" || exit 1
eval set -- "${FLAGS_ARGV}"
# shellcheck disable=SC2154
if [ "${FLAGS_create}" -eq "${FLAGS_TRUE}" ]; then
#....
fi

Bash focus point

set -e, -u, -o pipefail

set -euo pipefail

Bash Manuel

Special parameter

https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Special-Parameters

Snippets

xargs template

Define a place holder with the -I option

echo "this is a basic xargs example" | xargs -I {} echo "you said: {}"

run a bash function with the timeout command

function my_function {
    until kubectl get -n kube-system deploy/my_dep ; do
        echo "Wait my_dep deployment ..."
        sleep 5
    done

    # Do someting
    
}

export -f my_function
timeout 20s bash -c my_function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment