SHELL
# export on source
set -a; source .env; set +a
visudo
myron ALL=(ALL) NOPASSWD:ALL
. <(flux completion bash) # completions
# debugging
set -xv
cmd | tee $(date '+%Y-%m-%d-%H%M%S-name.log')
# iTerm search. find 500s in log
HTTP[^\s]+\s5\d\d\s
# auto expanding
echo {a,b}.name
ap -i ./inv-lab.yml ./playbook-upgrade.yml \
-l $(echo {k7,ph}.overton-design.com|tr ' ' ',')
# exit on error
#! /bin/bash -eu
set -o pipefail
# dump env and functions
typeset
# see definition
type myfunc
# tail from the beginning
tail -0F
# xargs
echo webserver loadbalancer database | tr ' ' '\n' \
| xargs -I % -P 3 bash -c 'ansible-playbook $1.yml' -- %
# remove root from list of files
for f in /sys/devices/system/cpu/vulnerabilities/*; \
do echo "${f##*/} -" $(cat "$f"); done
BASH
^u - kill left of cursor
^k - kill right
^w - kill word to left
ZSH
Links
Shell Countdown
countdown() { declare desc="A simple countdown. Source: https://superuser.com/a/611582" local seconds="${1}" local d=$(($(date +%s) + "${seconds}")) while [ "$d" -ge `date +%s` ]; do echo -ne "$(date -u --date @$(($d - `date +%s`)) +%H:%M:%S)\r"; sleep 0.1 done }
Counting
# steps for i in $(seq 0 5 20); do echo $i # Outputs: 0 5 10 15 20 done for i in {0..20..5}; do echo $i # Outputs: 0 5 10 15 20 done
sed sed_i
function sed_i { if test "$(uname)" == "Darwin" then sed -i '' "$@" return $? else sed -i "$@" return $? fi }
Sequences
for i in {0..9}; do echo $i; done for i in `seq 1 5`;do x; done
completions
complete #list complete -F _asdf b # assign to function # load from tools like eksctl . <(eksctl completion bash)
Find base dir
DIR=$(dirname $([ -L $0 ] && readlink $0 || echo $0)) DIR=$(cd $DIR>/dev/null; pwd -P)
find all extensions in a directory
find . -type f -path '*/.git*' -prune -o -print \ |while read f; \ do echo ${f##*.}; done \ |sort -u
Remove 0x0d line-endings
sed_i 's/\r$//g' /entrypoint
Wait for file
until [ -f /tmp/examplefile.txt ] do sleep 5 done echo "File found" exit
If else
if
Local variables
func() { local arg1="${1}" local x="${2%/}"
If user does not have a shell
su - -s /bin/bash tomcat8
Arrays
declare -A aliases aliases=( [mainline]='1 1.13 latest' [stable]='1.12' ) versions=( */ ) versions=( "${versions[@]%/}" ) # add to existing
Resolve path
# Need to play with this self="$(basename "$BASH_SOURCE")" cd "$(dirname "$(readlink -f "$BASH_SOURCE")")"
Check remote shell vars
echo env |ssh -T mdc |grep TARG
Test arg processing
#!/bin/sh echo ------------- for f in "$@"; do echo \"$f\" done #!/bin/sh function command () { echo echo "command line: $@" echo "command output:" echo "-------------------------------" "$@" code=$? echo "-------------------------------" return $code } ./mro.2 a b c ./mro.2 "a b" c ./mro.2 a "b c" command ./mro.2 a b c command ./mro.2 "a b" c command ./mro.2 a "b c"
Option processing
OPTIND=1 while getopts "hdp" opt; do case "$opt" in h) usage ;; '?') usage ;; d) PROFILE=dev ;; p) PROFILE=prod ;; :) echo "Option -$OPTARG requires an argument." >&2 exit 1 ;; esac done shift "$((OPTIND-1))" # Shift off the options and optional --.
Gen random strings
export CHART_BUCKET=s3://example-chart-store-$(cat /dev/random | LC_ALL=C tr -dc "[:alpha:]" | tr '[:upper:]' '[:lower:]' | head -c 32) openssl rand 16 -hex node -e "console.log(require('crypto').randomBytes(256).toString('base64'));" uuidgen pwgen 50 1 -s > passphrase # Or use KeePassX generator ruby -rsecurerandom -e 'puts SecureRandom.hex(32)' cat /proc/sys/kernel/random/uuid