Skip to content

Instantly share code, notes, and snippets.

@brianoflan
brianoflan / realpath.sh
Created April 4, 2018 20:30
Define realpath in a way that works like 'readlink -f ...' even on macOS
_realpath() {
local return=0
local result=''
local arg=${1:-}
[[ $arg ]] || die "ERROR: _realpath: missing operand"
local pwd0=$(pwd)
local base tmp
cd "$arg";
base=$(basename "$(pwd)")
@brianoflan
brianoflan / git
Created January 12, 2018 23:45
Wrap git to add a ticket number to every commit (put this earlier in your PATH than /usr/bin/git)
#!/bin/bash
REAL_GIT=${REAL_GIT-/usr/bin/git}
TICKET_PREFIX=TICKET-PREFIX- # Like JIRA-, REDMINE-, CLEARQUEST- or something.
TICKET_REGEX="${TICKET_PREFIX}[0-9][0-9]*"
main() {
local command=$1
shift
if [[ commit == $command ]] || [[ ticket == $command ]] ; then
get_ticket
@brianoflan
brianoflan / swgit
Created December 22, 2017 15:08
~/bin/swgit to switch .git folders in your current folder
#!/bin/bash
main() {
local switch_to=$1
local what_repo=$(basename "$(pwd)")
local topd=$(pwd)
local top=true
[[ -d .git ]] || top=false
while [[ $top == false ]] ; do
cd .. || die "ERROR: Failed to find a top git repo root (a parent folder with a .git subfolder, stopped at $tmp)."
@brianoflan
brianoflan / touch_p.sh
Created November 29, 2017 18:28
Touch a file - and create any necessary folder structure to do so
#!/bin/bash
touch_p() { mkdir -p "$(dirname $1)"; touch "$1"; }
@brianoflan
brianoflan / en_de_crypt.sh
Created November 2, 2017 15:57
Encrypt and decrypt short strings with the user's default SSH key pair
#!/bin/bash
# Encrypts and decrypts short strings with the user's default SSH key pair.
# USAGE:
# encrypt file: cat some_short_file.txt | encrypt_str
# encrypt variable: echo "$some_variable" | encrypt_str
# decrypt file: cat something.encrypted | decrypt_str
# decrypt variable: echo "$string" | decrypt_str
#
@brianoflan
brianoflan / dir_diff.lib.sh
Created October 30, 2017 19:08
Diff two very similar directories
#!/bin/bash
IFF_ARGS='-dbB'
dir_diff() {
local a=$1
local b=$2
exit=0
rm -rf a b diff &> /dev/null || true
local thisn=a
@brianoflan
brianoflan / git_status_all.sh
Created October 12, 2017 17:36
Check a folder structure for Git workspaces and report if they are behind their remotes
#!/bin/bash
fix=$1 ;
DEBUG=2 ;
limitLines=20 ;
maxDepth=15 ;
tmp=old ;
thisD=$(dirname "$0") ;
@brianoflan
brianoflan / dockdeb.sh
Last active October 3, 2017 02:12
For spinning up a lightweight Debian Docker server VM
#!/bin/bash
# With ability to mount a CIFS/Samba share the way one might
# with a cheap external hard drive attached to an expensive WiFi router.
main() {
set -x
# update-alternatives --list editor
sudo update-alternatives --set editor /usr/bin/vim.tiny
sudo apt-get -y update
@brianoflan
brianoflan / get_sslFingerprints.sh
Created September 27, 2017 02:00
How to get sha256 fingerprints for use with Jenkins Swarm client's -sslFingerprints option
#!/bin/bash
sslFingerprint=$(echo \
| openssl s_client -showcerts -connect $some_jenkins_server_fqdn_name:443 2> /dev/null \
| openssl x509 -noout -sha256 -fingerprint | awk -F'=' '{print $NF}')
# Note that you can strip the colons for a shorter fingerprint:
echo $sslFingerprint | tr -d ':'
# But you do not need to strip them.
@brianoflan
brianoflan / functions.sh
Created June 15, 2017 02:50
say_do, die, execute, etc.
#!/bin/bash
main() {
execute echo hi ;
execute true ;
# execute false || echo "$? from execute false" ;
# { execute false; } || echo "$? from execute false" ;
( execute false; ) || echo "$? from execute false" ;
say_do echo howdy ;
say_do true ;