Skip to content

Instantly share code, notes, and snippets.

@mjf
Last active October 14, 2019 07:27
Show Gist options
  • Save mjf/9f685eb69d2b47fdc3aa to your computer and use it in GitHub Desktop.
Save mjf/9f685eb69d2b47fdc3aa to your computer and use it in GitHub Desktop.
USA & USE - Useful Shell Aliases and Environment
#! /bin/sh
# USA - Useful Shell Aliases
# Copyright (C) 2015-2017 Matous Jan Fialka, <http://mjf.cz/>
# Released under the terms of "The MIT License"
#
# Common aliases (SELinux support)
#
alias lz='ls -aZ'
alias cp='cp -i'
alias mv='mv -i'
#
# File hierarchy
#
alias md='{ read d && mkdir -p "$d"; } <<<'
alias mcd='{ read d && md "$d" && cd "$d"; } <<<'
#
# Save/Restore Standard Descriptors (STDIN/STDOUT/STDERR)
#
alias stdin_save='exec 128>&1'
alias stdin_restore='exec 1>&128 128>&-'
alias stdout_save='exec 129>&1'
alias stdout_restore='exec 1>&129 129>&-'
alias stderr_save='exec 130>&2'
alias stderr_restore='exec 2>&130 130>&-'
#
# Terminal Sequence (VT100 ANSI)
#
alias tsc='tput sc'
alias trc='tput rc'
alias tupdate='IFS='\'';'\'' read -sdR -p$'\''\e[6n'\'' LINE COL'
alias tlines='tput lines'
alias tline='tupdate && echo ${LINE#*[}'
alias tcols='tput cols'
alias tcol='tupdate && echo ${COL#*[}'
alias tsetpos='tput cup'
#
# Terminal Sequence (VT100 ANSI) Mouse for Readline :-)
#
alias tmouseon='printf -- '\''\033[2001;2002;2003;2004;2005;2006s\033[?2001;2002;2003h'\'''
alias tmouseoff='printf -- '\''\033[?2001;2002;2003;2004;2005;2006r'\'''
#
# Processes
#
alias swaps='grep '\''^VmSwap:'\'' /proc/[0-9]*/status |grep -v '\'' 0 kB$'\'' |sort -rnk2 |cut -d/ -f3| while read pid; do printf -- '\''%d\t%d\t%s\n'\'' "$pid" $(grep ^VmSwap /proc/$pid/status |cut -f2 |tr -d '\'' kB'\'') "$(sed '\''s/\x0/ /g'\'' /proc/$pid/cmdline)"; done'
#
# Helpers
#
export DEBUG= # fill in (on demand)
alias debug='! [ $DEBUG ] && : ||'
# e.g.
# export DEBUG=yes
# debug ls /
alias log='history -p !! >> ${LOGFILE:-${HOME:-/tmp}/log}'
# e.g.
# echo We want to log this echo.
# log
alias pipe='sed '\''s/[ \t]*#.*$//; /^[ \t]*$/d'\'' <<- into |'
# e.g.:
# pipe
# A
# B
# C
# into
# tac
alias ?='echo $USER@$HOSTNAME:$PWD $$ $?'
#
# Command improvers
#
alias mirror='wget -r -N -l inf -e robots=off --user-agent='\'''\'' -q -k -np -p'
alias where='find ${PATH//:/ } -name'
# Uniq assorted
alias uniqa='awk '\''!x[$0]++'\'''
#
# CD/DVD burning
#
export CDROM= # fill in
export DVD= # fill in
alias mkiso='mkisofs -J --iso-level 4 -R'
alias burnisocd='cdrecord driveropts=burnfree dev=/dev/${CDROM:-/dev/cdrom}'
alias burnisodvd='growisofs -dvd-compat -Z /dev/${DVDRW:-/dev/sr0}'
#
# RPM
#
alias rpm_vendor='rpm -q --qf '\''%{V}-%{R}-%{ARCH}\t%{VENDOR}\n'\'''
#
# GRUB2
#
alias grub2-menu-entries='awk -F\'\'' '\''$1=="menuentry " {printf "%d\t%s\n", i++, $2}'\'' /etc/grub2.cfg'
#
# Formatting
#
alias chmodfmt='sed '\''
s/.\(.........\).*/\1/
h
y/rwsxtSTlL-/IIIIIOOOOO/
x
s/..\(.\)..\(.\)..\(.\)/|\1\2\3/
y/sStTlLx-/IIIIIIOO/
G
s/\n\(.*\)/\1;OOO0OOI1OIO2OII3IOO4IOI5IIO6III7/
:k
s/|\(...\)\(.*;.*\1\(.\)\)/\3|\2/
tk
s/^0*\(..*\)|.*/\1/
q
'\'''
# e.g.: echo 'drwxrwxrwt.' |chmodfmt
alias hexstr='perl -ne '\''s/([0-9a-f]{2})/print chr hex $1/gie'\'''
alias strhex='xxd -c1 |cut -d'\'' '\'' -f2 |tr -d '\''\n'\'''
alias unhexcode='sed '\''s/'\''${HEXCHAR:-%}'\''\([0-9a-fA-F][0-9a-fA-F]\)/\\\\\x\1/g'\'' |xargs printf -- '\''%b\n'\'''
#
# Processing
#
export ROTCHARS= # fill in
alias rotchars='ROTCHARS="${ROTCHARS:-/-\|}"; printf -- '\''%c\b'\'' "${ROTCHARS:$((++ROTINDEX%${#ROTCHARS})):1}"'
# e.g.: export ROTCHARS='.oOo'
# e.g.: export ROTCHARS='>|<'
#
# Paging
#
alias pg='vi -R +'\''set nowrap'\'' +'\''map <Space> <PageDown>'\'' +'\''map q :quit<CR>'\'''
# e.g.: find / -name foo |pg -
#
# Manuals
#
alias psman='man -t'
# e.g.: psman logrotate.conf |gv -
alias pdfman='(read args && psman $args |ps2pdf - -) <<<'
# e.g.: pdfman 3\ fopen |acroread -
#
# OpenSSL helpers
#
alias x509='openssl x509 -noout -text'
alias rsa='openssl rsa -noout -text'
alias cipher='read -p "enter ${CIPHER:-aes-256-cbc} encryption data:" -s b && echo && read -p "enter ${CIPHER:-aes-256-cbc} encryption data password:" -s p && echo && openssl enc -pass pass:"$p" -a -${CIPHER:-aes-256-cbc} <<<"$b"'
# e.g.: CIPHER=des3 cipher # enter 'foo' and then 'bar'
alias decipher='openssl enc -a -d -${CIPHER:-aes-256-cbc} <<<'
# e.g.: CIPHER=des3 decipher U2FsdGVkX19A3fOUCXBbc8WTvQzsgGrxZo2QfRpPV5Q= # enter 'bar'
#
# Printing
#
alias lpr8='lpr -o document-format='\''text/plain;charset=utf-8'\''' # print UTF-8
alias print='a2ps -1Bf8 --borders=no'
#
# MySQL specific
#
alias mysql_strip_definers='sed '\''s/\(\(\*\) *\![0-9]\+ \)\?DEFINER=[^ ]\+ \+/\2/'\'''
#
# HTTP specific
#
alias HEAD='curl -skI'
alias HEAD_TRACE='curl -skIL'
alias GET='curl -sk'
alias GET_TRACE='curl -skL'
#
# mupdf(1) specific
#
export MUPDF_SESSION= # fill in
alias mupdf_list='cut -d '\'' '\'' -f2- <~/.mupdf.saved."${MUPDF_SESSIONL-latest}" |xargs -n 1 basename |${PAGER:-more}'
alias mupdf_restore='cut -d '\'' '\'' -f 2- <~/.mupdf.saved."${MUPDF_SESSION:-latest}" |xargs -d '\''\n'\'' -n 1 -P 0 mupdf'
alias mupdf_save='ps --no-headers -C mupdf -o cmd >~/.mupdf.saved."${MUPDF_SESSION:-latest}"'
# e.g.: MUPDF_SESSION='<somename>' mupdf_save
#
# Kernel
#
alias lsmods='find /lib/modules/$(uname -r) -type f -name '\''*.ko'\'' |sed '\''s,.*/kernel/,,;s,/,.,g;s/\.ko$//'\'' |sort'
# e.g.: lsmods |grep net.ipv4.tcp
alias modinfoparms='grep '\'''\'' /sys/module/*/parameters/* 2>/dev/null |cut -d/ -f4,6- |sed '\''s|/| |;s/:/=/'\'' |awk '\''s!=$1||NR==1{s=$1;if(p){print p};p=$0;next}{sub($1,"",$0);p=p""$0;}END{print p}'\'''
#
# Networking
#
alias ip4rev='sed '\''s/\([0-9]\+\)\.\([0-9]\+\)\.\([0-9]\+\)\.\([0-9]\+\)/\4.\3.\2.\1/'\'' <<<'
alias asn4='{ read __ && dig IN TXT +short $(ip4rev $__).asn.rspamd.com |tr -d \" |tr \| '\'' '\'' |tr a-z A-Z; } <<<'
alias dix='dig -x'
alias vendor='(read m && curl -s -Acurl http://www.macvendorlookup.com/api/v2/$m/pipe |cut -d'\''|'\'' -f5) <<<'
alias hwaddr='(read i && ip -o l sh dev $i| grep -Eo '\''([0-9a-f]{2}:){5}[0-9a-f]{2}'\'' |head -1) <<<'
#
# HAProxy
#
alias haproxy_password='read -s -p'\''Enter Password: '\'' && python3 -c '\''import crypt;print(crypt.crypt("'\''$REPLY'\''",crypt.mksalt(crypt.METHOD_MD5)))'\'''
# e.g.: echo MyStRoNgPaSsWoRd |haproxy_password
#
# Kubernetes
#
alias k8s-getns='echo $(kubectl config view |grep namespace:) |cut -d'\'' '\'' -f2'
alias k8s-setns='kubectl config set-context $(kubectl config current-context) --namespace'
alias k8s-explain-fmt='sed '\''y/\t/ /;s/^\([^ \t][^:]\+:\)\(.*\)/\x1b[1m\1\x1b[0m\2/;s/<\([^>]\+\)>/<\x1b[36m\1\x1b[0m>/g'\'''
#
# YAML to JSON and JSON to YAML converters
#
alias yaml2json='ruby -ryaml -rjson -e '\''puts JSON.pretty_generate(YAML.load(ARGF))'\'''
alias json2yaml='ruby -ryaml -rjson -e '\''puts YAML.dump(JSON.parse(STDIN.read))'\'''
# vi:ft=sh
#! /bin/sh
# USE - Useful Shell Environment
# Copyright (C) 2017 Matous Jan Fialka, <http://mjf.cz/>
# Released under the terms of "The MIT License"
#
# Prompt String
#
declare -x PS1='\u@\H:\w'
case "$TERM" in
'xterm'*)
declare -x PS1="\\[\\e]0;$PS1\\a\\]$PS1"
;;
esac
declare -x PS1="$PS1\\n\\\$ "
#
# Localization
#
declare -x LC_ALL='C'
declare -x LANG='C'
#
# Editing
#
declare -x EDITOR='ex'
declare -x VISUAL='vi'
#
# Glob Expansion
#
declare -x GLOBIGNORE=`printf '.[!/.]*:..[!/]*:*/.[!/.]*:*/..[!/]*:*[\001-\037\177]*:-*'`
# vi:ft=sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment