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 / latest-version.md
Last active March 18, 2024 16:22
Print all latest patch-level version

How to get the latest patch-level version from a list of distinct versions using bash and awk. This idea was taken from https://stackoverflow.com/a/21103864/1006369

Supose you have the following list of versions:

v1.22.8
v1.22.9
v1.22.10   <-- latest v1.22 series
v1.23.1
@caruccio
caruccio / kpfz.md
Created February 6, 2024 12:13
Kubectl port-forward with fzf

If you have fuzzy finder, use the following functions to filter for multiple kubernetes services and port-foward using a stable and unique local port.

Put the code below into your ~/.bashrc (or any place you source from)

kpfz ()
{ 
    local svcs=$(FZF_DEFAULT_COMMAND="command kubectl get svc -o json $_NS | svc-ports" fzf -m --ansi --no-preview -1 --exact --select-1 --query="$@");
    if [ -z "$svcs" ]; then
 return;
@caruccio
caruccio / pause.sh
Last active April 20, 2024 15:41
Pause processes during night
#!/bin/bash
#
# This script will stop a list of running proccesses (var PROCS), disable all monitors and CPUs (except cpu-0).
#
# Usage:
# pause [duration]
#
# With no parameters it will pause right alway.
# If [duration] is specified, it will detect for inactivity and pause, in an infinite loop. Use CTRL+C to exit.
# [duration] accepts the same as the command sleep.
@caruccio
caruccio / termseq.py
Created January 18, 2024 01:03
Simple Python terminal sequence
class TermSequence:
'''Reference: https://stackoverflow.com/a/33206814/1006369
'''
sequence = []
# standard 4-bit color codes
black = 30
red = 31
green = 32
yellow = 33
@caruccio
caruccio / multi-value-array.sh
Last active January 6, 2024 22:30
Bash multi-value arrays
#
# Given an array of multi-value items, transform each value into a new array replacing its
# separator for a space, then access each individual inner-value by index:
#
ITEMS=(
cup:blue:3
phone:black:15
pen:red:1
)
@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 / 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 / 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
#