Skip to content

Instantly share code, notes, and snippets.

View caruccio's full-sized avatar
😐
state = not in mood

Mateus Caruccio caruccio

😐
state = not in mood
View GitHub Profile
@caruccio
caruccio / .inputrc
Created November 21, 2023 15:15
Disable bash multiline paste protection
# From bash(1) man page
# enable-bracketed-paste (On)
# When set to On, readline configures the terminal to insert each paste into the editing buffer as a single string of characters, instead of treating each character as if it had been read from
# the keyboard. This prevents readline from executing any editing commands bound to key sequences appearing in the pasted text.
#
set enable-bracketed-paste off
@caruccio
caruccio / bash-long-options.sh
Last active November 17, 2023 13:07
Shell long-options
#!/bin/bash
SERVER=''
SHORT_PARAM='-s' # short option name
LONG_PARAM='--server' # long option name
while [ $# -gt 0 ]; do
case "${1}" in
$SHORT_PARAM=*|$SHORT_PARAM*|$LONG_PARAM=*|$LONG_PARAM) ## matches both`[-s|--server]=value` and `[-s|--server] value`
param="${1#$SHORT_PARAM}" ## removes `-s` from the front of $1
@caruccio
caruccio / getopts.md
Last active November 3, 2023 22:07
Read shell options with positional arguments

This example shows how to read options and positional arguments from a bash script (same principle can be applied for other shells).

# some global var we want to overwrite with options
force=false
help=false
log=info
ARGS=() ### this array holds any positional arguments, i.e., arguments not started with dash

while [ $# -gt 0 ]; do
@caruccio
caruccio / bash-regex.md
Last active October 2, 2023 15:00
Bash regex conditionals

In bash you can use [[ pattern =~ regex ]] to match on a conditional:

if [[ "hello world" =~ ^hell ]]; then
  echo Match
else
  echo No match
fi
@caruccio
caruccio / metatuple.py
Created September 22, 2012 04:49
metattuple - recursively creating a namedtuple class from a dict object using a metaclass
''' metatuple - Recursive namedtuple from arbitrary dict
After 2 hours of intensive coding and some tequila sips I found
a "simple" solution to create a namedtuple from any dictionary,
recursivelly creating any necessary namedtuple.
Probably there are tons of easiest ways of doing that, like some
well documented method or standart function in the python library,
but that wouldn't be fun.'''
@caruccio
caruccio / colors.sh
Last active April 26, 2023 00:06
Create env vars for somes ANSI colors
function mkcolor()
{
export $1="${2}" ${1}_E="\[${2}\]"
case "$1" in
BOLD|RESET)
return
;;
*)
export ${1}_B="$(tput bold)${2}" # Bold
@caruccio
caruccio / kubectl-extract
Created January 18, 2023 15:08
Extract secret value
#!/bin/bash
#
# Install: copy to path
# $ cp kubectl-extract /usr/local/bin
#
# Usage:
# $ kubectl extract -n default my-secret
#
@caruccio
caruccio / shopt-toggler.sh
Created March 22, 2016 14:13
How to toggle a shopt flag
# Lets assume you need to turn on a shopt (dotglob, for example),
# do some work and finally turn it back to it's original state.
# First we check if it's on already (shopt -q <flag>),
# and set a flag variable in case it was.
# Otherwise we turn it on and ensure the flag is clear (unset <flag>).
shopt -q dotglob && dotglob=1 || { shopt -s dotglob && unset dotglob; }
## Here we can do whatever we need to do...
# Returns true (0) if $1 is on $PATH, false (1) otherwise
# Handles path expansions as expected
# Example: is_dir_on_path ~/bin
is_dir_on_path()
{
local path
local dir
# convert $PATH to array
@caruccio
caruccio / miniargs.py
Last active December 26, 2022 00:22
Really small python command line arg parser, now with parameter validation®
# -*- coding: utf-8 -*-
from __future__ import print_function
class MiniArgs(object):
__getattr__ = lambda *a, **ka: None
def __init__(self, argv, valid=None):
def normalize_name(name):
return name.lstrip('-').replace('-','').replace('+','')