Skip to content

Instantly share code, notes, and snippets.

@brianoflan
brianoflan / MkTemp.sh
Last active December 16, 2016 17:36
mktemp (mktmp, tmp, TMPDIR, make temporary file, make temporary folder, make tmp, make temporary dir, make temporary directory)
#!/bin/bash
mktmpd() {
local tmpd=`mktemp -d "${TMPDIR:-/tmp}/tmp.d.XXXXXXXXXX"` ;
echo "$tmpd" ;
}
MkTemp() {
local tmpf=`mktemp "${TMPDIR:-/tmp}/tmp.XXXXXXXXXX"` ;
echo "Temporary file: $tmpf" ;
@brianoflan
brianoflan / who_full.sh
Last active December 16, 2016 17:36
Which folder is most full? (ls_fullest, df, du, df -h, du -ks)
#!/bin/bash
ls_fullest() {
ls -a | egrep -v '^[.][.]?$' | while read e ; do x=`df -h "$e" | tail -1 | awk '{print $NF}'` ; [[ "$x" == "$tgt" ]] && sudo du -ks "$e" 2>/dev/null ; done | sort -n -k 1 | tail
}
tgt='/var' ;
cd "$tgt" ;
ls_fullest ;
@brianoflan
brianoflan / BASH_script.sh
Last active December 16, 2016 17:46
Handy ways to avoid set -e -x (execute, vexecute, die, log, elog, stderr)
#!/bin/bash
set -u ;
log=$(basename "$0" | sed -e 's/[.][^.]*$//') ;
elog="$log.stderr.log" ;
log="$log.stdout.log" ;
die() { echo $1 ; exit 1 ;
}
@brianoflan
brianoflan / setHostname.sh
Last active December 16, 2016 17:44
Set hostname on CentOS on AWS EC2 instance (hostnamectl set-hostname, /etc/hosts, /etc/sysconfig/network, /etc/cloud/cloud.cfg)
#!/bin/bash
# # # USAGE: hostname -f ; /bin/bash setHostname.sh new.hostname.to.be.set.in.some.domain ; hostname -f
# # # Per the official but mildly inaccurate AWS EC2 hostname docs
# # # (https://aws.amazon.com/premiumsupport/knowledge-center/linux-static-hostname-rhel7-centos7/)
setHostname() {
local fqdn=$1 ;
@brianoflan
brianoflan / subRvm
Last active December 16, 2016 17:43
Isolate RVM environment (rbenv, hash the Gemfile and don't re-fetch all those gems if they don't change, broken)
#!/bin/bash
[[ $isolateRvm ]] || isolateRvm=true ;
useRubyVersion=2.2.2 ;
main() {
local use_rvm_or_rbenv='rvm' ;
rvm -v &> /dev/null || use_rvm_or_rbenv='rbenv' ;
local x='' ;
if [[ "rbenv" == "$use_rvm_or_rbenv" ]] ; then
@brianoflan
brianoflan / ReplaceJenkinsPlugins.sh
Last active December 16, 2016 17:42
Replace Jenkins Plugins (root, chown, chmod, hpi, jpi)
#!/bin/bash
# As root:
date=`date -u +'%Y%m%dT%H%M%Sz'` ;
new=/jenkins/new_plugins ;
old="/jenkins/old_plugins_$date" ;
j=/var/lib/jenkins ;
main() {
@brianoflan
brianoflan / man augtool
Last active June 20, 2016 18:19
Augeas Man Page
AUGTOOL(1) Augeas AUGTOOL(1)
NAME
augtool - inspect and modify configuration files
SYNOPSIS
augtool [OPTIONS] [COMMAND]
DESCRIPTION
Augeas is a configuration editing tool. It parses configuration files in their native formats and transforms them into a tree. Configuration changes are
@brianoflan
brianoflan / fix_aws.sh
Last active December 16, 2016 17:41
Fix AWS EC2 instance root umask for awscli and cloud-utils (and euca2ools, hiera-eyaml, gem, umask, chmod)
#!/bin/bash
main() {
forceRoot ;
local pkg='' ;
for pkg in awscli hiera-eyaml ; do
gem uninstall $pkg --executables ;
done ;
for pkg in awscli ; do
yum -y install $pkg ;
@brianoflan
brianoflan / root_or_death.sh
Last active December 16, 2016 17:40
Fail unless run by root (id, forceRoot, exit)
#!/bin/bash
forceRoot() {
[[ "$(id -u)" == "0" ]] || { echo "ERROR: Must run $0 as root." ; exit 1 ; }
}
forceRoot ;
#
@brianoflan
brianoflan / du_ks_sort.sh
Last active December 16, 2016 17:40
How full is full? (who_full, ls_fullest, df, du, df -h, du -ks, sort, only one partition)
#!/bin/bash
initDuks() {
tgt=$1 ;
[[ $tgt ]] || tgt='/var' ;
cd "$tgt" ;
duks ;
}