From 67afb66eb1a2e7a1f7c912bdeec002de7b25d91d Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Wed, 17 Aug 2016 17:38:45 +1200 Subject: Move simple Bash/pdksh functions into POSIX sh Have only translated the scripts that translate readily into POSIX sh for now. More complex stuff like that bd/pd/sd/ud navigation for Bash doesn't port as easily, mostly because there isn't an analogue for the "local" keyword in POSIX. --- sh/profile | 5 +++++ sh/shrc | 7 +++++++ sh/shrc.d/bc.sh | 7 +++++++ sh/shrc.d/diff.sh | 4 ++++ sh/shrc.d/ed.sh | 26 ++++++++++++++++++++++++++ sh/shrc.d/gdb.sh | 4 ++++ sh/shrc.d/gpg.sh | 10 ++++++++++ sh/shrc.d/hgrep.sh | 16 ++++++++++++++++ sh/shrc.d/keychain.sh | 3 +++ sh/shrc.d/mkcd.sh | 4 ++++ sh/shrc.d/mysql.sh | 19 +++++++++++++++++++ sh/shrc.d/pwgen.sh | 8 ++++++++ sh/shrc.d/rcsdiff.sh | 4 ++++ sh/shrc.d/scp.sh | 14 ++++++++++++++ sh/shrc.d/scr.sh | 6 ++++++ sh/shrc.d/sudo.sh | 5 +++++ sh/shrc.d/tmux.sh | 19 +++++++++++++++++++ sh/shrc.d/vim.sh | 13 +++++++++++++ 18 files changed, 174 insertions(+) create mode 100644 sh/shrc create mode 100644 sh/shrc.d/bc.sh create mode 100644 sh/shrc.d/diff.sh create mode 100644 sh/shrc.d/ed.sh create mode 100644 sh/shrc.d/gdb.sh create mode 100644 sh/shrc.d/gpg.sh create mode 100644 sh/shrc.d/hgrep.sh create mode 100644 sh/shrc.d/keychain.sh create mode 100644 sh/shrc.d/mkcd.sh create mode 100644 sh/shrc.d/mysql.sh create mode 100644 sh/shrc.d/pwgen.sh create mode 100644 sh/shrc.d/rcsdiff.sh create mode 100644 sh/shrc.d/scp.sh create mode 100644 sh/shrc.d/scr.sh create mode 100644 sh/shrc.d/sudo.sh create mode 100644 sh/shrc.d/tmux.sh create mode 100644 sh/shrc.d/vim.sh (limited to 'sh') diff --git a/sh/profile b/sh/profile index e9e974e0..e190d6ba 100644 --- a/sh/profile +++ b/sh/profile @@ -9,3 +9,8 @@ for profile in "$HOME"/.profile.d/*.sh ; do . "$profile" done unset -v profile + +# If the shell is interactive, and ~/.shrc exists, source that too +case $- in + *i*) [ -f "$HOME"/.shrc ] && . "$HOME"/.shrc ;; +esac diff --git a/sh/shrc b/sh/shrc new file mode 100644 index 00000000..e46ec2d8 --- /dev/null +++ b/sh/shrc @@ -0,0 +1,7 @@ +# Load all the POSIX-compatible functions from ~/.shrc.d; more advanced shells +# like bash will have their own functions +for shrc in "$HOME"/.shrc.d/*.sh ; do + [ -e "$shrc" ] || continue + . "$shrc" +done +unset -v shrc diff --git a/sh/shrc.d/bc.sh b/sh/shrc.d/bc.sh new file mode 100644 index 00000000..643678ac --- /dev/null +++ b/sh/shrc.d/bc.sh @@ -0,0 +1,7 @@ +# This function is only applicable if bc(1) has the non-POSIX -q option +command bc -q &0 2>&0 || return + +# Don't print the bc(1) welcome message +bc() { + command bc -q "$@" +} diff --git a/sh/shrc.d/diff.sh b/sh/shrc.d/diff.sh new file mode 100644 index 00000000..2c752c8d --- /dev/null +++ b/sh/shrc.d/diff.sh @@ -0,0 +1,4 @@ +# Use a unified format for diff(1) by default +diff() { + command diff -u "$@" +} diff --git a/sh/shrc.d/ed.sh b/sh/shrc.d/ed.sh new file mode 100644 index 00000000..c7afb176 --- /dev/null +++ b/sh/shrc.d/ed.sh @@ -0,0 +1,26 @@ +# Add a colon prompt to ed when a command is expected rather than text; makes +# it feel a lot more like using ex. Only do this when stdin is a terminal, +# however. Also try and use -v for more verbose error output, and rlwrap(1) if +# it's available. +ed() { + + # We're only adding options if input is from a terminal + if [ -t 0 ] ; then + + # Colon prompt (POSIX) + set -- -p : "$@" + + # Verbose if available (not POSIX) + if ed -sv - &0 2>&0 ; then + set -- -v "$@" + fi + fi + + # Execute the ed(1) call, in a wrapper if appropriate and with the + # concluded options + if [ -t 0 ] && hash rlwrap 2>/dev/null ; then + command rlwrap ed "$@" + else + command ed "$@" + fi +} diff --git a/sh/shrc.d/gdb.sh b/sh/shrc.d/gdb.sh new file mode 100644 index 00000000..ec9d4137 --- /dev/null +++ b/sh/shrc.d/gdb.sh @@ -0,0 +1,4 @@ +# Don't print the GDB copyright message on every invocation +gdb() { + command gdb -q "$@" +} diff --git a/sh/shrc.d/gpg.sh b/sh/shrc.d/gpg.sh new file mode 100644 index 00000000..62d123ea --- /dev/null +++ b/sh/shrc.d/gpg.sh @@ -0,0 +1,10 @@ +# Wrapper around gpg(1) to stop ``--batch'' breaking things +gpg() { + # shellcheck disable=SC2048 + case $* in + *--ed*|*--gen-k*|*--sign-k*) + set -- --no-batch "$@" + ;; + esac + command gpg "$@" +} diff --git a/sh/shrc.d/hgrep.sh b/sh/shrc.d/hgrep.sh new file mode 100644 index 00000000..b6143b37 --- /dev/null +++ b/sh/shrc.d/hgrep.sh @@ -0,0 +1,16 @@ +# Search shell history file for a pattern. If you put your whole HISTFILE +# contents into memory, then you probably don't need this, as you can just do: +# +# $ history | grep PATTERN +# +hgrep() { + if [ "$#" -eq 0 ] ; then + printf >&2 'hgrep: Need a pattern\n' + exit 2 + fi + if ! [ -n "$HISTFILE" ] ; then + printf >&2 'hgrep: No HISTFILE\n' + exit 2 + fi + grep "$@" "$HISTFILE" +} diff --git a/sh/shrc.d/keychain.sh b/sh/shrc.d/keychain.sh new file mode 100644 index 00000000..82f83473 --- /dev/null +++ b/sh/shrc.d/keychain.sh @@ -0,0 +1,3 @@ +# If GPG_TTY is set, update it +[ -n "$GPG_TTY" ] || return +GPG_TTY=$(tty) diff --git a/sh/shrc.d/mkcd.sh b/sh/shrc.d/mkcd.sh new file mode 100644 index 00000000..6342d4a6 --- /dev/null +++ b/sh/shrc.d/mkcd.sh @@ -0,0 +1,4 @@ +# Create a directory and change into it +mkcd() { + mkdir -p -- "$1" && builtin cd -- "$1" +} diff --git a/sh/shrc.d/mysql.sh b/sh/shrc.d/mysql.sh new file mode 100644 index 00000000..00b6930c --- /dev/null +++ b/sh/shrc.d/mysql.sh @@ -0,0 +1,19 @@ +# If a file ~/.mysql/$1.cnf exists, call mysql(1) using that file. Otherwise +# just run MySQL with given args. Use restrictive permissions on these files. +# Examples: +# +# [client] +# host=dbhost.example.com +# user=foo +# password=SsJ2pICe226jM +# +# [mysql] +# database=bar +# +mysql() { + if [ -f "$HOME"/.mysql/"$1".cnf ] ; then + shift + set -- --defaults-extra-file="$HOME"/.mysql/"$1".cnf "$@" + fi + command mysql "$@" +} diff --git a/sh/shrc.d/pwgen.sh b/sh/shrc.d/pwgen.sh new file mode 100644 index 00000000..7ba056e5 --- /dev/null +++ b/sh/shrc.d/pwgen.sh @@ -0,0 +1,8 @@ +# Set some defaults for pwgen(1), because its defaults are to give me a long +# list of relatively short passwords, when I generally want only one good one +pwgen() { + if ! (($#)) ; then + set -- --secure -- "${PWGEN_LENGTH:-15}" "${PWGEN_COUNT:-1}" + fi + command pwgen "$@" +} diff --git a/sh/shrc.d/rcsdiff.sh b/sh/shrc.d/rcsdiff.sh new file mode 100644 index 00000000..18b1d324 --- /dev/null +++ b/sh/shrc.d/rcsdiff.sh @@ -0,0 +1,4 @@ +# Use a unified format for rcsdiff(1) by default +rcsdiff() { + command rcsdiff -u "$@" +} diff --git a/sh/shrc.d/scp.sh b/sh/shrc.d/scp.sh new file mode 100644 index 00000000..cc46b229 --- /dev/null +++ b/sh/shrc.d/scp.sh @@ -0,0 +1,14 @@ +# Wrap scp(1) to check for missing colons +scp() { + # shellcheck disable=SC2048 + if [ "$#" -ge 2 ] ; then + case $* in + *:*) ;; + *) + printf >&2 'scp(): Missing colon, probably an error\n' + return 2 + ;; + esac + fi + command scp "$@" +} diff --git a/sh/shrc.d/scr.sh b/sh/shrc.d/scr.sh new file mode 100644 index 00000000..255b9322 --- /dev/null +++ b/sh/shrc.d/scr.sh @@ -0,0 +1,6 @@ +# Create a temporary directory and change into it, to stop me putting stray +# files into $HOME, and making the system do cleanup for me. Single optional +# argument is the string to use for naming the directory; defaults to "scr". +scr() { + cd -- "$(mktd "${1:-scr}")" +} diff --git a/sh/shrc.d/sudo.sh b/sh/shrc.d/sudo.sh new file mode 100644 index 00000000..a5883168 --- /dev/null +++ b/sh/shrc.d/sudo.sh @@ -0,0 +1,5 @@ +# Add the -H parameter to sudo(8) calls, always use the target user's $HOME +sudo() { + [ "$1" != -v ] && set -- -H "$@" + command sudo "$@" +} diff --git a/sh/shrc.d/tmux.sh b/sh/shrc.d/tmux.sh new file mode 100644 index 00000000..bd954be8 --- /dev/null +++ b/sh/shrc.d/tmux.sh @@ -0,0 +1,19 @@ +# Attach to existing tmux session rather than create a new one if possible +tmux() { + + # If given any arguments, just use them as they are + if [ "$#" -gt 0 ] ; then + : + + # If a session exists, just attach to it + elif command tmux has-session 2>/dev/null ; then + set -- attach-session -d + + # Create a new session with an appropriate name + else + set -- new-session -s "${TMUX_SESSION:-default}" + fi + + # Execute with concluded arguments + command tmux "$@" +} diff --git a/sh/shrc.d/vim.sh b/sh/shrc.d/vim.sh new file mode 100644 index 00000000..fc04c99f --- /dev/null +++ b/sh/shrc.d/vim.sh @@ -0,0 +1,13 @@ +# If Vim exists on the system, use it instead of ex, vi, and view +command -v vim >/dev/null || return + +# Define functions proper +ex() { + vim -e "$@" +} +vi() { + vim "$@" +} +view() { + vim -R "$@" +} -- cgit v1.2.3