Bash from scratch

This commit is contained in:
John Mertz 2022-04-16 15:54:07 -04:00
parent 0ebac68a66
commit 1677aaeb3b
7 changed files with 405 additions and 0 deletions

42
bash/bash_aliases Normal file
View File

@ -0,0 +1,42 @@
# vim: ft=sh
# Vim stuff
alias :q="exit"
alias :wq="exit"
alias q="exit"
alias i="vim -c 'startinsert'"
# shortcuts
alias c="clear"
alias t="date +%T"
alias ll='ls -alh'
alias fuck='sudo "$BASH" -c "$(history -p !!)"'
# OS functions (TODO: should be broken out and pulled in based on OS)
alias apt-all="sudo apt-get update; sudo apt-get full-upgrade -y; sudo apt-get autoremove -y; sudo apt-get clean -y;"
alias rpm-all="rpm-ostree upgrade"
alias pip-upgrade="pip3 list --outdated --format=freeze | grep -v '^\-e' | cut -d = -f 1 | xargs -n1 pip3 install -U"
# Legacy mappings
alias ifconfig='ip addr'
# SSH hop script
alias pgen='ssh -A -t -i ~/.ssh/no_pass 10.10.0.1 ~/bin/pgen'
alias spgen='ssh -A -t -i ~/.ssh/no_pass 10.10.0.1 ~/bin/spgen'
# Prefer a Vim in a new terminal over gVim
alias gvim="/usr/bin/urxvt -e /bin/bash -c vim -i"
# Startup scripts
alias startsway='/home/jpm/scripts/sway/startsway.sh'
alias starti3='/home/jpm/scripts/i3/starti3.sh'
# Web shortcuts
alias papillon="/usr/bin/chromium --app='https://papillon.john.me.tz/hud.php?refresh=3600&theme=dark'"
# Force python3
alias pip="pip3"
alias python="python3"
# Backlight control
alias blc='/home/jpm/scripts/thinkpad/blc.pl --notify'

35
bash/bash_login Normal file
View File

@ -0,0 +1,35 @@
#!/bin/bash
# Load aliases
source ${HOME}/.config/bash/bash_aliases
# Configure static SSH Agent
export SSH_AUTH_SOCK=~/.ssh/ssh-agent.sock
if [ -e $SSH_AUTH_SOCK ]; then
rm $SSH_AUTH_SOCK
fi
ssh-agent -a "$SSH_AUTH_SOCK" >/dev/null
# Static/predictable Sway socket
export SWAYSOCK="/home/jpm/.config/sway-ipc.sock"
# Standardize language on en_US because of the ubiquity
export LANG="en_US.UTF-8"
export LANGUAGE="en_US.UTF-8"
export LC_CTYPE="en_US.UTF-8"
export LC_ALL="en_US.UTF-8"
# Dirty config dir
export XDG_DATA_HOME="${HOME}/.config"
# GUI themes
export GTK_THEME="Oled"
export PROFILE_NAME="Oled"
export QT_QPA_PLATFORMTHEME="qt5ct"
export QT_QPA_PLATFORM="wayland-egl;wayland;xcb"
export DCONF=".config/dconf/user"
# Suggest session start command if last session is not known
if [ ! -e /home/jpm/.config/last_login_gui ]; then
echo "Use 'startsway' or 'starti3' to launch a GUI"
fi

9
bash/bash_logout Normal file
View File

@ -0,0 +1,9 @@
# vim: ft=sh
# Remove static SSH Agent
rm SSH_AUTH_SOCK
# when leaving the console clear the screen to increase privacy
if [ "$SHLVL" = 1 ]; then
[ -x /usr/bin/clear_console ] && /usr/bin/clear_console -q
fi

17
bash/bash_profile Normal file
View File

@ -0,0 +1,17 @@
# vim: ft=sh
# If running bash (vs. login prompt)
if [ -n "$BASH_VERSION" ]; then
# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
fi
THEME="dark"
# Perl junk to allow scripts to run for non-interactive sessions
PERL5LIB="/home/jpm/perl5/lib/perl5${PERL5LIB:+:${PERL5LIB}}"; export PERL5LIB;
PERL_LOCAL_LIB_ROOT="/home/jpm/perl5${PERL_LOCAL_LIB_ROOT:+:${PERL_LOCAL_LIB_ROOT}}"; export PERL_LOCAL_LIB_ROOT;
PERL_MB_OPT="--install_base \"/home/jpm/perl5\""; export PERL_MB_OPT;
PERL_MM_OPT="INSTALL_BASE=/home/jpm/perl5"; export PERL_MM_OPT;

102
bash/bashrc Normal file
View File

@ -0,0 +1,102 @@
# vim: ft=sh
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac
# Use static SSH Agent from profile
export SSH_AUTH_SOCK=~/.ssh/ssh-agent.sock
# agetty is set to automatically log me in on tty1
# Automatically launch GUI on tty1 after login
if [ "$(tty)" == '/dev/tty1' ]; then
. /home/jpm/.bash_login
LAST_GUI=`cat /home/jpm/.config/last_login_gui`
if [ -f "/home/jpm/.config/last_login_gui" ]; then
/home/jpm/scripts/${LAST_GUI}/start${LAST_GUI}.sh
fi
fi
# Setup editor
export EDITOR="vim"
# don't put duplicate lines or lines starting with space in the history.
shopt -s histappend
HISTCONTROL=ignoreboth
# append to the history file, don't overwrite it
# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=500000
HISTFILESIZE=1000000
# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize
# enable programmable completion features
if ! shopt -oq posix; then
if [ -f /usr/share/bash-completion/bash_completion ]; then
. /usr/share/bash-completion/bash_completion
elif [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi
fi
# Bell when prompt is returned to mark as urgent
PROMPT_COMMAND='printf "\a"; history -a'
# Reconfigure prompt after each return
PROMPT_COMMAND="$PROMPT_COMMAND; source /home/jpm/.config/bash/prompt.sh"
# Enable color support if possible
if [ -x /usr/bin/dircolors ]; then
test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
alias ls='ls --color=auto'
alias dir='dir --color=auto'
alias vdir='vdir --color=auto'
alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='egrep --color=auto'
export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'
fi
# Load aliases
if [ -f ~/.config/bash/bash_aliases ]; then
. ~/.config/bash/bash_aliases
fi
# Add snaps and flatpaks to path
#export XDG_DATA_DIRS="$XDG_DATA_DIRS:/usr/local/share:/usr/share:/var/lib/snapd/desktop:/var/lib/flatpak/exports/share"
# Perl configs
PERL5LIB="/home/jpm/perl5/lib/perl5${PERL5LIB:+:${PERL5LIB}}"; export PERL5LIB;
PERL_LOCAL_LIB_ROOT="/home/jpm/perl5${PERL_LOCAL_LIB_ROOT:+:${PERL_LOCAL_LIB_ROOT}}"; export PERL_LOCAL_LIB_ROOT;
PERL_MB_OPT="--install_base \"/home/jpm/perl5\""; export PERL_MB_OPT;
PERL_MM_OPT="INSTALL_BASE=/home/jpm/perl5"; export PERL_MM_OPT;
# Configure PATH
source "$HOME/.config/bash/path"
# Standardize language
export LANG="en_US.UTF-8"
export LANGUAGE="en_US:en"
export LC_ALL="en_US.UTF-8"
export LC_CTYPE="en_US.UTF-8"
# terminal type
export TERM="xterm"
# GUI options. Mostly to optimize Wayland
export QT_QPA_PLATFORM="wayland-egl;wayland;xcb"
export SDL_VIDEODRIVER="wayland"
export QML_IMPORT_PATH="/usr/lib/qt/qml"
export QT_PLUGIN_PATH="/usr/lib/qt/plugins"
export QML2_IMPORT_PATH="/usr/lib/qt/qml"
export MOZ_ENABLE_WAYLAND="1"
export MOZ_WEBRENDER="1"
export XDG_SESSION_TYPE="wayland"
export XDG_CURRENT_DESKTOP="sway"
export XDG_CONFIG_HOME=/"home/jpm/.config"
export SWAYSOCK="/home/jpm/.config/sway-ipc.sock"
export CHROME_EXECUTABLE="/usr/bin/chromium"

34
bash/path Normal file
View File

@ -0,0 +1,34 @@
# Local user bin
if [ -d "$HOME/bin" ] ; then
PATH="$HOME/bin:$PATH"
fi
if [ -d "$HOME/.local/bin" ] ; then
PATH="$HOME/.local/bin:$HOME/.local/bin/firefox:$PATH"
fi
if [ -d "$HOME/.local/share/applications" ] ; then
export PATH="$HOME/.local/share/applications/:$PATH"
fi
# Private scripts
if [ -d "$HOME/.private-scripts" ] ; then
PATH="$HOME/.private-scripts:$PATH"
fi
# Public scripts
if [ -d "$HOME/scripts" ] ; then
PATH="$HOME/scripts:$PATH"
fi
# Flutter
PATH="$HOME/flutter/bin/:$PATH"
# Python
PATH="$PYENV_ROOT/bin:$PATH"
export PYENV_ROOT="$HOME/.pyenv"
if command -v pyenv 1>/dev/null 2>&1; then
eval "$(pyenv init -)"
fi
# Perl
PATH="/home/jpm/perl5/bin:$PATH"
# Export PATH
export PATH="$PATH"

166
bash/prompt.sh Executable file
View File

@ -0,0 +1,166 @@
# vim: ft=sh
# Explain prompt
ARG=$1
if [[ $ARG == 'h' || $ARG == '-h' || $ARG == 'help' || $ARG == '--help' || $ARG == '?' ]]; then
ARG="-h"
elif [[ $ARG != '' ]]; then
echo "Invalid argument: $ARG"
exit 0
fi
# Default colours
# 0 default, 1 red 2 green 3 yellow 4 blue 5 magenta 6 cyan 7 white (grey)
PC="0" # prompt default
TC="3" # time
EC="7" # venv
CC="1" # chroot
UC="2" # user
RC="1" # root user
HC="4" # host
SC="1" # ssh host
DC="6" # dir
GC="5" # git
# Declare the SET name (as used by `setterm`) and PS string for colour options
# Reference them later like: ${COLOURS["5","SET"]} or ${COLOURS["2","PS"]}
declare -A COLOURS='(
[0,SET]="default"
[0,PS]="\[\033[0;0m\]"
[1,SET]="red"
[1,PS]="\[\033[0;31m\]"
[2,SET]="green"
[2,PS]="\[\033[0;32m\]"
[3,SET]="yellow"
[3,PS]="\[\033[0;33m\]"
[4,SET]="blue"
[4,PS]="\[\033[0;34m\]"
[5,SET]="magenta"
[5,PS]="\[\033[0;35m\]"
[6,SET]="cyan"
[6,PS]="\[\033[0;36m\]"
[7,SET]="white"
[7,PS]="\[\033[0;37m\]"
)'
# Get current git branch
function parse_git_branch() {
BRANCH=`git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'`
if [ ! "${BRANCH}" == "" ]
then
STAT=`parse_git_dirty`
echo "${COLOURS[\"$GC\",\"PS\"]}(${BRANCH}${STAT})${COLOURS[\"0\",\"PS\"]}"
else
echo ""
fi
}
# Get git status
function parse_git_dirty {
status=`git status 2>&1 | tee`
dirty=`echo -n "${status}" 2> /dev/null | grep "modified:" &> /dev/null; echo "$?"`
untracked=`echo -n "${status}" 2> /dev/null | grep "Untracked files" &> /dev/null; echo "$?"`
ahead=`echo -n "${status}" 2> /dev/null | grep "Your branch is ahead of" &> /dev/null; echo "$?"`
newfile=`echo -n "${status}" 2> /dev/null | grep "new file:" &> /dev/null; echo "$?"`
renamed=`echo -n "${status}" 2> /dev/null | grep "renamed:" &> /dev/null; echo "$?"`
deleted=`echo -n "${status}" 2> /dev/null | grep "deleted:" &> /dev/null; echo "$?"`
bits=''
if [ "${renamed}" == "0" ]; then
bits=">${bits}"
fi
if [ "${ahead}" == "0" ]; then
bits="*${bits}"
fi
if [ "${newfile}" == "0" ]; then
bits="+${bits}"
fi
if [ "${untracked}" == "0" ]; then
bits="?${bits}"
fi
if [ "${deleted}" == "0" ]; then
bits="x${bits}"
fi
if [ "${dirty}" == "0" ]; then
bits="!${bits}"
fi
if [ "${bits}" != "" ]; then
echo " ${bits}"
else
echo ""
fi
}
# Comment - allow for command history to be copied directly to bash script
PS1="${COLOURS["$PC","PS"]}# "
# Time
PS1="${PS1}${COLOURS["$TC","PS"]}\\t "
if [[ $ARG == '-h' ]]; then
setterm --foreground ${COLOURS["$TC","SET"]}; echo "time"; setterm --foreground default
fi
# Python venv
if [ $VIRTUAL_ENV ]; then
VENV="${VIRTUAL_ENV##*/}"
VENV=`echo $VENV | sed -r 's/.*/[\0]/'`
PS1="${PS1}${COLOURS["$EC","PS"]}${VENV} "
if [[ $ARG == '-h' ]]; then
setterm --foreground ${COLOURS["$EC","SET"]}; echo "venv"; setterm --foreground default
fi
fi
# Debian Chroot
if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
PS1="${PS1}${COLOURS["$CC","PS"]}<$(cat /etc/debian_chroot)>"
if [[ $ARG == '-h' ]]; then
setterm --foreground ${COLOURS["$CC","SET"]}; echo "debian chroot"; setterm --foreground default
fi
fi
# User
if [[ `whoami` == 'root' ]]; then
UC=$RC
fi
PS1="${PS1}${COLOURS["$UC","PS"]}\\u"
if [[ $ARG == '-h' ]]; then
setterm --foreground ${COLOURS["$UC","SET"]}; echo "user"; setterm --foreground default
fi
# @
PS1="${PS1}${COLOURS["$PC","PS"]}@"
# Host
if [[ $SSH_TTY != '' ]]; then
HC="${SC}" # Host colour
if [[ $ARG == '-h' ]]; then
setterm --foreground ${COLOURS["$HC","SET"]}; echo "host"; setterm --foreground default
fi
else
if [[ $ARG == '-h' ]]; then
setterm --foreground ${COLOURS["$HC","SET"]}; echo "host (ssh)"; setterm --foreground default
fi
fi
PS1="${PS1}${COLOURS["$HC","PS"]}\\h"
# :
PS1="${PS1}${COLOURS["$PC","PS"]}:"
# Dir
PS1="${PS1}${COLOURS["$DC","PS"]}\\w "
if [[ $ARG == '-h' ]]; then
setterm --foreground ${COLOURS["$DC","SET"]}; echo "directory"; setterm --foreground default
fi
# Git
GIT=$(parse_git_branch)
if [[ $GIT != '' ]]; then
PS1="${PS1}${COLOURS["$GC","PS"]}${GIT}"
if [[ $ARG == '-h' ]]; then
setterm --foreground ${COLOURS["$GC","SET"]}; echo "git branch status"; setterm --foreground default
fi
fi
# \n - start input on new line, again to support copy-paste into bash script
PS1="${PS1}${COLOURS["$PC","PS"]}\\n"
export PS1=$PS1