# E.g. after `.mkshrc` is called, perhaps in `~/.mkshrc.local` # export GOPATH=$HOME/code/gopath # export GOROOT=$HOME/go/1.13.5 # set_go set_go() { export GOPATH="${GOPATH:-$HOME/go}" export PATH="$GOPATH/bin:$PATH" GOROOT="${GOROOT:-/usr/local/go}" _gobin="${GOROOT}/bin" if [ -e "$_gobin" ] ; then export PATH="$_gobin:$PATH" fi } set_editor() { if [ -z "$EDITORS" ] ; then export EDITORS="ed vi vim" fi export EDITOR="$(which $EDITORS 2>/dev/null | tail -n1)" export VISUAL="$EDITOR" # Set an alias for our editor of choice alias vi="$EDITOR" alias vim="$EDITOR" alias edit="$EDITOR" alias e="$EDITOR" } set_pager() { export PAGER="$(which cat more less 2>/dev/null | tail -n1)" } set_lang() { export CHARSET="$1"; export LANG="$1"; } # Usage debugstarttls $ipaddr:$port debugstarttls() { openssl s_client -starttls smtp -crlf -connect "$1" "$2" } randmacgen() { if [ "$(uname)" = "FreeBSD" ] ; then LC_COLLATE=c fi tr -c -d '0123456789abcdef' < /dev/urandom \ | head -c 12 \ | sed 's!^M$!!;s!\-!!g;s!\.!!g;s!\(..\)!\1:!g;s!:$!!' echo "" } # First argument is the interface on which to scan ipv6_local_hosts() { if ! [ -z $1 ] ; then interface="$1" ping6 -c 3 ff02::2%$interface \ | grep 'bytes from' \ | awk '{print $4}' \ | sort \ | uniq \ | sed -e's/:$//' else echo "You need to specify an interface" fi } set_nocaps() { setxkbmap -layout "us,de" setxkbmap -option ctrl:nocaps setxkbmap -option shift:breaks_caps setxkbmap -option numpad:microsoft setxkbmap -option grp:alt_space_toggle } # Usage: dir_oct:file_oct path setperms() { _perm="$1"; shift dir_perms=$(echo "$_perm" | awk -F: '{print $1}') file_perms=$(echo "$_perm" | awk -F: '{print $2}') find "$@" -type f -print0 | xargs -0 chmod "$file_perms" find "$@" -type d -print0 | xargs -0 chmod "$dir_perms" } set_title() { printf $'\033]0;'"%s"$'\007' "$1" } set_my_title() { _load="Load Avg: $(_sys_load)" _free_mem="M Free: $(_sys_memory)" _uptime="Uptime: $(_sys_uptime)" set_title "$(id -un)[$(hostname)] -- $_load $_free_mem $_uptime" } _sys_memory() { ! [ -e $(which vmstat) ] && return if [ `uname` = "Linux" ] ; then vmstat | tail -n1 | awk '{print $4/1024}' | sed -e's/\..*$//g' elif [ `uname` = "FreeBSD" ] ; then vmstat | tail -n1 | awk '{print $5/1024}' | sed -e's/\..*$//g' elif [ `uname` = "OpenBSD" ] ; then vmstat | sed -n '$p' | awk '{print $4}' | sed 's/M$//' fi } _sys_load() { # Works on BSD too, neat. uptime | sed -E 's/^.*load averages?: ([0-9]+\.[0-9]+).*$/\1/g' } _sys_uptime() { # Took awhile to figure out, but this works on BSD as well uptime | sed -E -e's/^.*up[ ]+//g' -e's/^([^,]*),.*/\1/g' -e's/^([^ ]+)[ ]*(.).*/\1\2/g' } timestamp() { date +%m.%d.%y_%H.%M.%S } checkSSHAgent() { ssh_agent_conf="$HOME/.ssh/agent" if [ -e "$ssh_agent_conf" ] ; then . "$ssh_agent_conf" fi if ! ps -eo pid | grep -q "$SSH_AGENT_PID" \ || ! [ -e "$ssh_agent_conf" ] \ || [ -z "$SSH_AGENT_PID" ] ; \ then ssh-agent -s | grep -v echo > "$ssh_agent_conf" . "$ssh_agent_conf" fi } passenv() { _f="$(mktemp)" pass "$@" > "$_f" . "$_f" rm -f "$_f" } ###################################################################### # Tmux utilities _tmux_session() { session="" window_name="" working_directory="$(pwd)" while [ $# -gt 0 ] ; do case $1 in --) shift ; break;; -s) session="$2" ; shift ; shift ;; -n) window_name="$2" ; shift ; shift ;; -w) working_directory="$2" ; shift ; shift ;; *)echo "Invalid option $1" ; return ;; esac ; done cd "$working_directory" || { echo "Cannot change to: $working_directory" ; return; } tmux new-session -s "$session" -n "$window_name" -c "$working_directory" \;\ "$@" } _tmux_dev() { session="" working_directory="$(pwd)" window_name="editor" while [ $# -gt 0 ] ; do case $1 in --) shift ; break;; -s) session="$2" ; shift ; shift ;; -n) window_name="$2" ; shift ; shift ;; -w) working_directory="$2" ; shift ; shift ;; *)echo "Invalid option $1" ; return ;; esac ; done cd "$working_directory" || { echo "Cannot change to: $working_directory" ; return; } _tmux_session -s "$session" -w "$working_directory" -n "$window_name" \ -- \ send-keys -t 0 "$EDITOR" \; send-keys -t 0 Enter \;\ new-window -n "shell" \;\ new-window -n "git" \;\ send-keys -t 0 "git status" \; send-keys -t 0 Enter \;\ "$@" } _tmux_servers_split_commands() { n=0 server_cmds="" while [ $# -gt 0 ] ; do if [ $n -eq 0 ] ; then server_cmds='send-keys "ssh \"'"$1"'\"" \; send-keys Enter \; ' else server_cmds="${server_cmds} split-window \; "'send-keys "ssh \"'"$1"'\"" \; send-keys Enter \; ' fi n="$(echo "$n+1" | bc)" shift done echo "$server_cmds" } _tmux_servers() { session="" working_directory="$HOME" servers="" layout="even-vertical" while [ $# -gt 0 ] ; do case $1 in --) shift ; break;; -s) session="$2" ; shift ; shift ;; -w) working_directory="$2" ; shift ; shift ;; -servers) servers="$2"; shift ; shift ;; -l) layout="$2"; shift ; shift ;; *)echo "Invalid option $1" ; return ;; esac ; done cd "$working_directory" || { echo "Cannot change to: $working_directory" ; return; } layout="select-layout $layout ';' set-window-option -g synchronize-panes on ';'" eval _tmux_session -s "\$session" -w "\$working_directory" -n "main" \ -- \ $(_tmux_servers_split_commands $servers) \ $layout \ "\$@" \ }