Skip to content

Instantly share code, notes, and snippets.

View forestbaker's full-sized avatar

input - process - output forestbaker

View GitHub Profile
@forestbaker
forestbaker / collapsing_functions.sh
Last active December 5, 2015 09:05
Collapsing Function Examples and More!
#!/usr/bin/env bash
# From Bash Hackers - http://wiki.bash-hackers.org/howto/collapsing_functions
# The first time you run chatter(), the function redefines itself based on the value of verbose. Thereafter, chatter doesn't check $verbose, it simply is.
# Further calls to the function reflect its collapsed nature.
# If verbose is unset, chatter will echo nothing, with no extra effort from the developer.
[[ $1 = -v || $1 = --verbose ]] && verbose=1
chatter() {
if [[ $verbose ]]; then
@forestbaker
forestbaker / load_binaries_into_array
Last active September 29, 2015 17:22
preload executable binaries into array by looping through directories in $PATH
# for loop on directories in PATH
for foo in ${PATH//:/ }; do
done
# use mapfile to load binaries
# just loads up element 0
mapfile _binary <${PATH//:/\/\* }
@forestbaker
forestbaker / shell_style_guide
Last active November 24, 2015 17:19
shell style guide & design philosophy
Complex Shell Script Outline
----------------------------
header
begin script
preamble:
- sanity checks
- create shell environ
- log & debug
error handling
check dependencies
@forestbaker
forestbaker / flock_lock_spock
Last active September 29, 2015 17:35
use flock to create an exclusive file lock like a pinch on the neck from mr. spock
http://www.kfirlavi.com/blog/2012/11/06/elegant-locking-of-bash-program/
Simple
#########################################
#!/bin/bash
# exit if flock fails.
set -e
# Wait for lock on /var/lock/.myscript.exclusivelock (fd 200) for 10 seconds
@forestbaker
forestbaker / closure_pattern
Last active April 18, 2023 18:54
closure in bash ... ?
# closure - defined by Peter Landin in 1964
# closure - a function, plus a pointer to that functions scope
# In programming languages, closures (also lexical closures or function closures) are a technique for
# implementing lexically scoped name binding in languages with first-class functions. Duh.
# Operationally, a closure is a record storing a function[a] together with an environment:[1]
# A mapping associating each free variable of the function (variables that are used locally, but defined in an enclosing scope)
# with the value or storage location to which the name was bound when the closure was created.
@forestbaker
forestbaker / triple_deck_check
Created September 29, 2015 17:55
the triple decker super checker
# fast & efficient triple check:
# existence, directory, writable || or error and exit
[[ -e $DIR && -d $DIR && -w $DIR ]] || { echo >&2 "Unable to access $DIR - check directory path / permissions?" ; exit 1 ; }
# exists , file, readable
[[ -s $FILE && -d $FILE && -r $FILE ]] || { echo >&2 "Unable to access $FILE - check file path / permissions?" ; exit 1 ; }
# exists , file, writable
[[ -s $FILE && -d $FILE && -w $FILE ]] || { echo >&2 "Unable to access $FILE - check file path / permissions?" ; exit 1 ; }
@forestbaker
forestbaker / Debug_RW_Tempfile
Created September 29, 2015 19:10
DEBUG - write to & read from same file
needs file descriptor and something like this
exec 10>$temp
<$temp>
#!/bin/bash -
# track path & filename for tempfile
temp="./example.tmp"
@forestbaker
forestbaker / ssh_sampler
Last active October 3, 2015 21:04
SSH client command samples
# one line command below
ssh -X -o UserKnownHostsFile=/dev/null -o CheckHostIP=no -o StrictHostKeyChecking=no \
-i /Users/veryv/.ssh/google_compute_engine -A -p 22 [email protected] --
@forestbaker
forestbaker / greptastic_flavorites
Last active November 24, 2015 19:29
greptastic flavorites
# display a random number based on the number of lines in a file
echo $(($RANDOM%`grep -c '$' ~/.bash_history`))
# replace COMMAND to find all the libboost libraries it is using
lsof | grep 'COMMAND.*mem.*libboost'
# display contents of file without comments or empty lines, also removes "in-line" comments
# this is an example of a reason to NOT USE IN LINE COMMENTS
egrep -v '#|^$' /etc/mysql/my.cnf
@forestbaker
forestbaker / admin_flavorites
Created October 1, 2015 01:01
admin flavorites - commands about commands processes services
# diplay alphabetically sorted list of processes with open files
# 'file' means: regular file, directory, block special, character special, executing text reference, library, stream or network file (Internet socket, NFS file or UNIX domain socket)
lsof | awk '{print $1}' | sort -u
# display all files that apache2 has open
#
lsof -c apache2 | less