Last active
August 5, 2019 19:52
-
-
Save dbuschman7/22cbb42d27689a53fb9cfb74e2f11381 to your computer and use it in GitHub Desktop.
bash_functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/bash | |
# | |
# BASIC | |
# ################### | |
export EDITOR=/usr/bin/vim | |
export SHELL=/bin/bash | |
export PAGER=less | |
# erase duplicated entries, ignore entries that are duplicates or begin with spaces | |
export HISTCONTROL="erasedups:ignoreboth" | |
export HISTSIZE=50000 | |
function removeFromPath() { | |
export PATH=$(echo ${PATH} | sed -E -e "s;:$1;;" -e "s;$1:?;;") | |
} | |
alias listening="netstat -na | grep LISTEN" | |
# Find a file up a dir | |
function upsearch () { | |
slashes=${PWD//[^\/]/} | |
directory="$PWD" | |
for (( n=${#slashes}; n>0; --n )) | |
do | |
test -e "$directory/$1" && echo "$directory/$1" && return $directory | |
directory="$directory/.." | |
done | |
} | |
function re-source() { | |
. ~/.bashrc | |
} | |
# | |
# COLORS | |
# ################ | |
export PS1="\[\033[36m\]\u\[\033[m\]@\[\033[32m\]\h:\[\033[33;1m\]\w\[\033[m\]\$ " | |
export CLICOLOR=1 | |
export LSCOLORS=ExFxBxDxCxegedabagacad | |
alias ls='ls -GFh' | |
export RED="\e[31m" | |
export YELLOW="\e[0;33m" | |
export GREEN="\e[0;32m" | |
export OCHRE="\e[38;5;95m" | |
export BLUE="\e[0;37m" | |
export WHITE="\e[0;37m" | |
export RESET="\e[0m" | |
# | |
# JDKs | |
# ################ | |
function removeFromPath() { | |
export PATH=$(echo ${PATH} | sed -E -e "s;:$1;;" -e "s;$1:?;;") | |
} | |
function showJdk() { | |
/usr/libexec/java_home | |
} | |
function setJdk() { | |
if [ $# -ne 0 ]; then | |
removeFromPath '/System/Library/Frameworks/JavaVM.framework/Home/bin' | |
if [ -n "${JAVA_HOME+x}" ]; then | |
removeFromPath ${JAVA_HOME}/bin | |
fi | |
export JAVA_HOME=`/usr/libexec/java_home -v $@` | |
export PATH=${JAVA_HOME}/bin:${PATH} | |
echo "" | |
java -version | |
echo "" | |
fi | |
} | |
function listJdks() { | |
/usr/libexec/java_home -V | |
} | |
if [ -f /usr/libexec/java_home ]; then | |
export HISTORY=10000 | |
echo "" | |
echo "Settings JDK ..." | |
setJdk 1.8 | |
else | |
echo "" | |
echo -n "Configured Jdk is " | |
java -version | |
javac -version | |
echo "" | |
fi | |
# | |
# Docker | |
# ############## | |
function docker-cleanup() { | |
docker images --no-trunc| grep '<none>' | awk '{print $3}' | xargs docker rmi | |
docker images --filter status=dead --filter=exited -aq | xargs docker rm -v | |
docker rm `docker ps -a | grep Exited | awk '{print $1 }'` | |
docker rm `docker ps -a | grep Created | awk '{print $1 }'` | |
docker volume rm $(docker volume ls -qf dangling=true) | |
for cont in $( docker images | awk '{ print $1; }' | sort | uniq ) | |
do | |
echo "**********************************************" | |
echo $cont | |
docker images | grep "$cont" | tail -n +5 | awk '{ print $3}' | xargs docker rmi | |
echo "**********************************************" | |
done | |
} | |
function docker-ps() { | |
docker ps -a | perl -ne 'chomp; @cols = split /\s{2,}/, $_; $name=pop @cols; printf "%-20s %-30s %-15s %s\n", $name, $cols[1], $cols[4], $cols[5]' | |
} | |
alias docker-top="docker-ps | awk '{if(NR>1)print}' | cut -c1-15 | tr '\\n' ' ' | xargs docker stats" | |
# | |
# OSX Helpers | |
# ################# | |
function reset-DNS() { | |
sudo killall -HUP mDNSResponder | |
} | |
function restart-audio() { | |
command sudo killall coreaudiod \ | |
&& sudo launchctl unload /System/Library/LaunchDaemons/com.apple.audio.coreaudiod.plist \ | |
&& sudo launchctl load /System/Library/LaunchDaemons/com.apple.audio.coreaudiod.plist \ | |
|| return echo 'Audio daemon restarted' | |
} | |
function ip-addr() { | |
ifconfig | grep -A 4 "en0" | grep "inet " | awk '{ print $2 }' | tr -d '[:space:]' | |
} | |
function http() { | |
docker run -it --rm --add-host=mac.local:$(ip-addr) clue/httpie $@ | |
} | |
# | |
# Kubernetes | |
# ################ | |
export NAMESPACE_FILE="${HOME}/.kube/namespace" | |
if [ -f $NAMESPACE_FILE ]; then | |
echo "NAMESPACE already defined" | |
else | |
echo "default" > $NAMESPACE_FILE | |
fi | |
function setPrompt() { | |
export AZ_AKS_CLUSTER=$(kubectl config current-context) | |
export NAMESPACE=$(cat $NAMESPACE_FILE) | |
PS1="[${AZ_AKS_CLUSTER}](${NAMESPACE}) \w\$ " | |
} | |
function contexts() { | |
kubectl config get-contexts | |
} | |
function namespace() { | |
if [[ ! $# -eq 1 ]]; then | |
echo "Must provide value to set for namespace" 1>&2 | |
else | |
KNOWN_NS=$(kg ns | grep -v NAME | awk '{ print $1 }' ) | |
FOUND=$(echo ${KNOWN_NS[@]} | grep -o "$1" | wc -w) | |
if [[ ! $FOUND -eq 1 ]]; then | |
echo "Unknown namespace - '$1' " | |
else | |
echo $1 > $NAMESPACE_FILE | |
export NAMESPACE=$(cat $NAMESPACE_FILE) | |
setPrompt | |
fi | |
fi | |
} | |
function namepsace() { | |
namespace $1 | |
} | |
export NAMESPACE="$(cat $NAMESPACE_FILE)" | |
echo "Namespace set : ${NAMESPACE}" | |
export PATH=~/etc/bin:$PATH | |
source <(kubectl completion bash) | |
setPrompt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment