Skip to content

Instantly share code, notes, and snippets.

View mattolenik's full-sized avatar

Matthew Olenik mattolenik

View GitHub Profile

Keybase proof

I hereby claim:

  • I am mattolenik on github.
  • I am matthewolenik (https://keybase.io/matthewolenik) on keybase.
  • I have a public key ASCQ3fh9LGPK1UYNYp6L_TGzifcHtI_bVCLZHJyIM-Vk-Qo

To claim this, I am signing this object:

@mattolenik
mattolenik / vagrantfile_install_gem.rb
Last active May 1, 2018 20:10
Install and use a new gem inside a Vagrantfile
gem_dir = "#{Dir.home}/.vagrant.d/gems/#{RUBY_VERSION}"
begin
name = "inifile"
version = "3.0.0"
Gem::Specification.find_by_name(name, version)
rescue Gem::LoadError
require "rubygems/dependency_installer"
installer = Gem::DependencyInstaller.new(:document => [], :install_dir => gem_dir)
installer.install(name, version)
@mattolenik
mattolenik / vagrantfile_ensure_plugins.rb
Created May 1, 2018 20:09
Automatically install missing Vagrant plugins upon 'vagrant up'
# Install any missing plugins upon 'vagrant up'
def ensure_plugins(required_plugins)
if ARGV[0] == 'up'
missing_plugins = required_plugins.select {|p| not Vagrant.has_plugin? p}
unless missing_plugins.empty?
plugins = missing_plugins.join(' ')
puts "Found missing plugins: #{plugins}, installing..."
exec "vagrant plugin install #{plugins} && vagrant up"
end
end
@mattolenik
mattolenik / find-shell.sh
Created May 23, 2018 01:36
Recursively find shell scripts by detecting shebang
# Caveat: grep can't match the beginning of the file, so this will catch files with a shebang on any line,
# such as a script being embedded in another kind of file. To get around this each file should be checked with head -n1
grep -Ir '^#!/.*sh' --exclude-dir 'node_modules' --exclude '*.tpl' ./* | awk -F: '{print $1}'
@mattolenik
mattolenik / bash-function-regex.sh
Created June 23, 2018 21:15
Bash regex pattern for matching bash functions
[[ 'function some_func ( ) {' =~ ^[[:blank:]]*(function[[:blank:]]+)?([\x30-\x39\x41-\x5A\x61-\x7A\xA0-\x19FF\+\-\.\^\/\?,%#_@:~]+)[[:blank:]]*\([[:blank:]]*\)[[:blank:]]*({)?$ ]]
#BASH_REMATCH results:
#0 - whole match
#1 - either 'function' or empty string
#2 - function name
#3 - either '{' or empty string
#Character ranges include pretty much all unicode and normal characters that bash will accept.
@mattolenik
mattolenik / sh
Created September 3, 2018 18:50
Use awk to uncomment specific lines of a file
# Strips n characters from the beginning of the line. This example uncomments a line in /etc/sudoers.
n=3
cat /etc/sudoers | awk '/^# %wheel ALL=\(ALL\) ALL/ {$0=substr($0,$n)} 1
@mattolenik
mattolenik / traps.sh
Created September 12, 2018 16:03
bash stack-based traps
#!/bin/bash
# From https://stackoverflow.com/questions/16115144/bash-save-and-restore-trap-state-easy-way-to-manage-multiple-handlers-for-trap
trap_stack_name() {
local sig=${1//[^a-zA-Z0-9]/_}
echo "__trap_stack_$sig"
}
extract_trap() {
echo ${@:3:$(($#-3))}
}
@mattolenik
mattolenik / trapadd.sh
Created September 12, 2018 16:03
bash trap add
# From https://stackoverflow.com/questions/3338030/multiple-bash-traps-for-the-same-signal
# note: printf is used instead of echo to avoid backslash
# processing and to properly handle values that begin with a '-'.
log() { printf '%s\n' "$*"; }
error() { log "ERROR: $*" >&2; }
fatal() { error "$@"; exit 1; }
# appends a command to a trap
#
@mattolenik
mattolenik / get_named_arg.sh
Last active September 16, 2018 01:04
Looks for "named arguments" in an array, which take the form key=value
# Searches for a named argument in the form of "key=value".
# Returns the value associated with the key.
#
get_named_arg() {
local key="$1"
local default="${2:-}"
shift 2
for pair in "$@"; do
if [[ $pair =~ ^([^=]+)=([^=]*)$ ]] && [[ ${BASH_REMATCH[1]} == "$key" ]]; then
printf %s "${BASH_REMATCH[2]}"
@mattolenik
mattolenik / tmuxx.sh
Created December 18, 2018 22:02
Single command to create/join a tmux session
# Creates/joins a shared session using session groups (-t option).
# This simplifies making a shared group. Just invoke tmuxx from anywhere
# and it'll always join to the same, shared session.
#
# Parameters:
# $1 the name of the session group, defaults to 'main'
tmuxx () {
local session="${1:-main}"
tmux new-session -t "$session"
}