Skip to content

Instantly share code, notes, and snippets.

@wrouesnel
Created February 2, 2023 04:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wrouesnel/e6236d0d4e32d62d05a9fcf70a3249b8 to your computer and use it in GitHub Desktop.
Save wrouesnel/e6236d0d4e32d62d05a9fcf70a3249b8 to your computer and use it in GitHub Desktop.
Generic bashrc with some sensible defaults
#!/bin/bash
# Common Linux bashrc
function prompt_add() {
if [ ! -z "$PROMPT_COMMAND" ]; then
export PROMPT_COMMAND="$PROMPT_COMMAND;$*"
else
export PROMPT_COMMAND="$*"
fi
}
export HISTFILESIZE=
export HISTSIZE=
export HISTCONTROL="ignoreboth"
export HISTIGNORE="ls;bg;fg;history"
export HISTTIMEFORMAT="%F_%T "
shopt -s cmdhist
prompt_add "history -a"
# Ensure our path modifications make sense
declare -A incoming_path
while read -r pelem || [ -n "$pelem" ]; do
incoming_path["$pelem"]=1
done < <(echo "${PATH//:/$'\n'}")
function pathadd() {
fpath=$(readlink -f "$1")
if [ ! -d "$fpath" ]; then
return
fi
# If the requested path is already present, don't add it.
if [[ -n "${incoming_path[$fpath]:-}" ]] ; then
return
fi
export PATH="$fpath:$PATH"
incoming_path["$fpath"]=1
}
function maybe_source() {
if [ -e "$1" ]; then
source "$1"
fi
}
# Enable .local as a bin dir
pathadd "$HOME/.local/bin"
pathadd "$HOME/bin"
pathadd "$HOME/.cargo/bin"
pathadd "$HOME/opt/go/bin"
if [ -d "$HOME/opt/go" ]; then
export GOROOT="$HOME/opt/go"
fi
pathadd "$HOME/src/go/bin"
# Set from editor defaults
if [ -x $(which code) ]; then
export VISUAL="$(which code) -n -w"
fi
# Add the Python3 debug egg to our python path
export PYTHONPATH="$HOME/opt/pycharm/debug-eggs/pycharm-debug-py3k.egg:$PYTHONPATH"
export GOPATH="$HOME/src/go"
# Shared .bashrc functions
# DEPRECATING
# alias manual-prime='sudo ~/pbuilder-repo/manual-prime'
# alias debian-gquilt='QUILT_PATCHES=debian/patches gquilt'
alias srsync='rsync --rsync-path="sudo rsync" '
alias clipboard='xsel -i --clipboard'
# Run a Google query
google() {
search=""
echo "Google Search: $@"
for term in $@; do
search="$search%20$term"
done
xdg-open "https://www.google.com/search?q=$search"
}
# download a directory into the current folder, omitting the usual wget noise
download_httpdir() {
# count the number of dirs of wget to break dirs
dircount=$(echo "$@" | sed 's/[[:alpha:]]\+\:\/\///g' | sed 's/^[^/]*//g' | tr '/' ' ' | wc -w)
wget -r -nH --cut-dirs=$dircount --no-parent --reject "index.html*" $@
}
# print an SSH private keys public part
ssh_pubkey() {
comment=$(ssh-keygen -l -f $@ | cut -d' ' -f3-)
echo -n $(ssh-keygen -y -f $@) $comment
echo
}
# print current id_rsa public key
current_pubkey() {
comment=$(ssh-keygen -l -f "$HOME/.ssh/id_rsa" | cut -d' ' -f3-)
echo -n $(ssh-keygen -y -f "$HOME/.ssh/id_rsa") $comment
echo
}
use-mitm-proxy() {
export http_proxy="http://127.0.0.1:8080"
export https_proxy="http://127.0.0.1:8080"
export HTTP_PROXY="http://127.0.0.1:8080"
export HTTPS_PROXY="http://127.0.0.1:8080"
}
# PyEnv
if [ -d "${HOME}/.pyenv" ]; then
export PYENV_ROOT="${HOME}/.pyenv"
if ! command -v pyenv >/dev/null ; then
export PATH="$PYENV_ROOT/bin:$PATH"
fi
#eval "$(pyenv init -)"
fi
# venv function for bashrc
# Activate a venv
function venv() {
op="$1"
shift
case "$op" in
on)
if [ -e "venv/bin/activate" ]; then
echo "deactivate to turn off venv" >&2
source venv/bin/activate
else
echo "No virtualenv found from current directory"
fi
;;
off)
deactivate
;;
*)
echo "Please specify on or off" >&2
esac
}
function rfc3339_now() {
python3 -c 'import pyrfc3339 ; import tzlocal ; import datetime ; print(pyrfc3339.generate(datetime.datetime.now(tz=tzlocal.get_localzone()), utc=False))'
}
function macaddr() {
printf '00:60:2F:%02X:%02X:%02X\n' $[RANDOM%256] $[RANDOM%256] $[RANDOM%256]
}
function libvirt_default_pool() {
virsh -c qemu:///session pool-dumpxml default | xmlstarlet sel -t -m '//path' -v . -n
}
function randomkey() {
local key_size=$1
shift
local key_size=${key_size:-256}
dd if=/dev/urandom count=$((key_size / 8)) bs=1 2>/dev/null | base64
}
# Generate a 128-bit identifier, encoded entirely in b32 lowercase. No special characters.
function safeid() {
python3 -c 'import uuid, base64 ; print(base64.b32encode(uuid.uuid4().bytes).lower().rstrip(b"=").decode())'
}
function check-perms() {
find "$1" -printf "%u:%g:%M\n" | sort | uniq
}
# Enable go-jira bash completions
eval "$(jira --completion-script-bash)"
# Enable the awscli
pathadd "$HOME/opt/awscli/bin"
complete -C "$HOME/opt/awscli/bin/aws_completer" aws
# Set a sensible default cloud regions
# AWS
export AWS_REGION="ap-southeast-2"
export AWS_DEFAULT_REGION="ap-southeast-2"
# GCP
export CLOUDSDK_COMPUTE_ZONE="australia-southeast1-a"
# Azure
export AZURE_DEFAULTS_LOCATION="australiaeast"
if [ ! -z "$PS_TAG" ]; then
export PS1="${PS_TAG}${PS1}"
fi
if [ ! -z "$AWS_ACCOUNT_NAME" ] && [ ! -z "$AWS_ACCOUNT_PROFILE" ] ; then
export PS1="[aws:$(tput setaf 3)${AWS_ACCOUNT_NAME}/${AWS_ACCOUNT_PROFILE}$(tput sgr0)] $PS1"
elif [ ! -z "$AWS_ACCESS_KEY_ID" ] && [ ! -z "$AWS_SECRET_ACCESS_KEY" ]; then
export AWS_ACCOUNT_ID=$(aws --output text sts get-caller-identity | cut -d' ' -f1)
export PS1="[aws:$(tput setaf 3)${AWS_ACCOUNT_ID}/...$(tput sgr0)] $PS1"
fi
# if podman is installed, alias docker to podman
if [ -x "$(which podman)" ] && [ ! -x "$(which docker)" ] ; then
alias docker='podman'
# let docker-compose work
export DOCKER_BUILDKIT=0
export DOCKER_HOST="unix://${XDG_RUNTIME_DIR}/podman/podman.sock"
fi
# Keep OAPI gen efficient
export OPENAPI_GENERATOR_VERSION="5.2.0"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment