Skip to content

Instantly share code, notes, and snippets.

View mattolenik's full-sized avatar

Matthew Olenik mattolenik

View GitHub Profile
@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 / 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 / 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 / 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 / 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 / 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)

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: