diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2016-08-17 17:38:45 +1200 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2016-08-17 17:38:45 +1200 |
commit | 67afb66eb1a2e7a1f7c912bdeec002de7b25d91d (patch) | |
tree | 0701c5da159452f229eb50a8b1f99387b31a01fe | |
parent | Only pull if rebase possible (diff) | |
download | dotfiles-67afb66eb1a2e7a1f7c912bdeec002de7b25d91d.tar.gz dotfiles-67afb66eb1a2e7a1f7c912bdeec002de7b25d91d.zip |
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.
-rw-r--r-- | Makefile | 6 | ||||
-rw-r--r-- | bash/bashrc.d/keychain.bash | 4 | ||||
-rw-r--r-- | bash/bashrc.d/scp.bash | 10 | ||||
-rw-r--r-- | bash/bashrc.d/scr.bash | 12 | ||||
-rw-r--r-- | bash/bashrc.d/vim.bash | 15 | ||||
-rw-r--r-- | pdksh/pdkshrc.d/diff.pdksh | 4 | ||||
-rw-r--r-- | pdksh/pdkshrc.d/ed.pdksh | 26 | ||||
-rw-r--r-- | pdksh/pdkshrc.d/gdb.pdksh | 4 | ||||
-rw-r--r-- | pdksh/pdkshrc.d/gpg.pdksh | 10 | ||||
-rw-r--r-- | pdksh/pdkshrc.d/keychain.pdksh | 4 | ||||
-rw-r--r-- | pdksh/pdkshrc.d/mkcd.pdksh | 4 | ||||
-rw-r--r-- | pdksh/pdkshrc.d/pwgen.pdksh | 8 | ||||
-rw-r--r-- | pdksh/pdkshrc.d/rcsdiff.pdksh | 4 | ||||
-rw-r--r-- | pdksh/pdkshrc.d/scp.pdksh | 9 | ||||
-rw-r--r-- | pdksh/pdkshrc.d/sudo.pdksh | 7 | ||||
-rw-r--r-- | pdksh/pdkshrc.d/tmux.pdksh | 19 | ||||
-rw-r--r-- | sh/profile | 5 | ||||
-rw-r--r-- | sh/shrc | 7 | ||||
-rw-r--r-- | sh/shrc.d/bc.sh (renamed from bash/bashrc.d/bc.bash) | 0 | ||||
-rw-r--r-- | sh/shrc.d/diff.sh (renamed from bash/bashrc.d/diff.bash) | 0 | ||||
-rw-r--r-- | sh/shrc.d/ed.sh (renamed from bash/bashrc.d/ed.bash) | 4 | ||||
-rw-r--r-- | sh/shrc.d/gdb.sh (renamed from bash/bashrc.d/gdb.bash) | 0 | ||||
-rw-r--r-- | sh/shrc.d/gpg.sh (renamed from bash/bashrc.d/gpg.bash) | 0 | ||||
-rw-r--r-- | sh/shrc.d/hgrep.sh (renamed from bash/bashrc.d/hgrep.bash) | 8 | ||||
-rw-r--r-- | sh/shrc.d/keychain.sh | 3 | ||||
-rw-r--r-- | sh/shrc.d/mkcd.sh (renamed from bash/bashrc.d/mkcd.bash) | 0 | ||||
-rw-r--r-- | sh/shrc.d/mysql.sh (renamed from bash/bashrc.d/mysql.bash) | 6 | ||||
-rw-r--r-- | sh/shrc.d/pwgen.sh (renamed from bash/bashrc.d/pwgen.bash) | 0 | ||||
-rw-r--r-- | sh/shrc.d/rcsdiff.sh (renamed from bash/bashrc.d/rcsdiff.bash) | 0 | ||||
-rw-r--r-- | sh/shrc.d/scp.sh | 14 | ||||
-rw-r--r-- | sh/shrc.d/scr.sh (renamed from pdksh/pdkshrc.d/scr.pdksh) | 7 | ||||
-rw-r--r-- | sh/shrc.d/sudo.sh (renamed from bash/bashrc.d/sudo.bash) | 4 | ||||
-rw-r--r-- | sh/shrc.d/tmux.sh (renamed from bash/bashrc.d/tmux.bash) | 2 | ||||
-rw-r--r-- | sh/shrc.d/vim.sh (renamed from pdksh/pdkshrc.d/vim.pdksh) | 4 |
34 files changed, 46 insertions, 164 deletions
@@ -269,9 +269,13 @@ install-readline : install -pm 0644 -- readline/inputrc "$(HOME)"/.inputrc install-sh : check-sh - install -m 0755 -d -- "$(HOME)"/.profile.d + install -m 0755 -d -- \ + "$(HOME)"/.profile.d \ + "$(HOME)"/.shrc.d install -pm 0644 -- sh/profile "$(HOME)"/.profile install -pm 0644 -- sh/profile.d/* "$(HOME)"/.profile.d + install -pm 0644 -- sh/shrc "$(HOME)"/.shrc + install -pm 0644 -- sh/shrc.d/* "$(HOME)"/.shrc.d install-subversion : install -m 0755 -d -- "$(HOME)"/.subversion diff --git a/bash/bashrc.d/keychain.bash b/bash/bashrc.d/keychain.bash deleted file mode 100644 index 40fe5d71..00000000 --- a/bash/bashrc.d/keychain.bash +++ /dev/null @@ -1,4 +0,0 @@ -# If GPG_TTY is set, update it -if [[ -n $GPG_TTY ]] ; then - GPG_TTY=$(tty) -fi diff --git a/bash/bashrc.d/scp.bash b/bash/bashrc.d/scp.bash deleted file mode 100644 index cd2f8521..00000000 --- a/bash/bashrc.d/scp.bash +++ /dev/null @@ -1,10 +0,0 @@ -# Wrap scp(1) to check for missing colons -scp() { - # shellcheck disable=SC2048 - if (($# >= 2)) && [[ $* != *:* ]] ; then - printf 'bash: %s: Missing colon, probably an error\n' \ - "$FUNCNAME" >&2 - return 2 - fi - command scp "$@" -} diff --git a/bash/bashrc.d/scr.bash b/bash/bashrc.d/scr.bash deleted file mode 100644 index bf2990fd..00000000 --- a/bash/bashrc.d/scr.bash +++ /dev/null @@ -1,12 +0,0 @@ -# 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() { - if (($# <= 1)) ; then - pushd -- "$(mktd "${1:-"$FUNCNAME"}")" - else - printf 'bash: %s: too many arguments\n' \ - "$FUNCNAME" >&2 - return 2 - fi -} diff --git a/bash/bashrc.d/vim.bash b/bash/bashrc.d/vim.bash deleted file mode 100644 index 37fc1871..00000000 --- a/bash/bashrc.d/vim.bash +++ /dev/null @@ -1,15 +0,0 @@ -# If Vim exists on the system, use it instead of ex, vi, and view -if ! hash vim 2>/dev/null ; then - return -fi - -# Define functions proper -ex() { - vim -e "$@" -} -vi() { - vim "$@" -} -view() { - vim -R "$@" -} diff --git a/pdksh/pdkshrc.d/diff.pdksh b/pdksh/pdkshrc.d/diff.pdksh deleted file mode 100644 index 2c752c8d..00000000 --- a/pdksh/pdkshrc.d/diff.pdksh +++ /dev/null @@ -1,4 +0,0 @@ -# Use a unified format for diff(1) by default -diff() { - command diff -u "$@" -} diff --git a/pdksh/pdkshrc.d/ed.pdksh b/pdksh/pdkshrc.d/ed.pdksh deleted file mode 100644 index f64f6dc6..00000000 --- a/pdksh/pdkshrc.d/ed.pdksh +++ /dev/null @@ -1,26 +0,0 @@ -# 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 availble (not POSIX) - if ed -sv - </dev/null >&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 ]] && command -v rlwrap >/dev/null ; then - command rlwrap ed "$@" - else - command ed "$@" - fi -} diff --git a/pdksh/pdkshrc.d/gdb.pdksh b/pdksh/pdkshrc.d/gdb.pdksh deleted file mode 100644 index ec9d4137..00000000 --- a/pdksh/pdkshrc.d/gdb.pdksh +++ /dev/null @@ -1,4 +0,0 @@ -# Don't print the GDB copyright message on every invocation -gdb() { - command gdb -q "$@" -} diff --git a/pdksh/pdkshrc.d/gpg.pdksh b/pdksh/pdkshrc.d/gpg.pdksh deleted file mode 100644 index 62d123ea..00000000 --- a/pdksh/pdkshrc.d/gpg.pdksh +++ /dev/null @@ -1,10 +0,0 @@ -# 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/pdksh/pdkshrc.d/keychain.pdksh b/pdksh/pdkshrc.d/keychain.pdksh deleted file mode 100644 index 40fe5d71..00000000 --- a/pdksh/pdkshrc.d/keychain.pdksh +++ /dev/null @@ -1,4 +0,0 @@ -# If GPG_TTY is set, update it -if [[ -n $GPG_TTY ]] ; then - GPG_TTY=$(tty) -fi diff --git a/pdksh/pdkshrc.d/mkcd.pdksh b/pdksh/pdkshrc.d/mkcd.pdksh deleted file mode 100644 index 6342d4a6..00000000 --- a/pdksh/pdkshrc.d/mkcd.pdksh +++ /dev/null @@ -1,4 +0,0 @@ -# Create a directory and change into it -mkcd() { - mkdir -p -- "$1" && builtin cd -- "$1" -} diff --git a/pdksh/pdkshrc.d/pwgen.pdksh b/pdksh/pdkshrc.d/pwgen.pdksh deleted file mode 100644 index 7ba056e5..00000000 --- a/pdksh/pdkshrc.d/pwgen.pdksh +++ /dev/null @@ -1,8 +0,0 @@ -# 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/pdksh/pdkshrc.d/rcsdiff.pdksh b/pdksh/pdkshrc.d/rcsdiff.pdksh deleted file mode 100644 index 18b1d324..00000000 --- a/pdksh/pdkshrc.d/rcsdiff.pdksh +++ /dev/null @@ -1,4 +0,0 @@ -# Use a unified format for rcsdiff(1) by default -rcsdiff() { - command rcsdiff -u "$@" -} diff --git a/pdksh/pdkshrc.d/scp.pdksh b/pdksh/pdkshrc.d/scp.pdksh deleted file mode 100644 index c3565f21..00000000 --- a/pdksh/pdkshrc.d/scp.pdksh +++ /dev/null @@ -1,9 +0,0 @@ -# Wrap scp(1) to check for missing colons -scp() { - # shellcheck disable=SC2048 - if (($# >= 2)) && [[ $* != *:* ]] ; then - printf 'ksh: scp: Missing colon, probably an error\n' >&2 - return 2 - fi - command scp "$@" -} diff --git a/pdksh/pdkshrc.d/sudo.pdksh b/pdksh/pdkshrc.d/sudo.pdksh deleted file mode 100644 index d6d91d12..00000000 --- a/pdksh/pdkshrc.d/sudo.pdksh +++ /dev/null @@ -1,7 +0,0 @@ -# Add the -H parameter to sudo(8) calls, always use the target user's $HOME -sudo() { - if [[ $1 != -v ]] ; then - set -- -H "$@" - fi - command sudo "$@" -} diff --git a/pdksh/pdkshrc.d/tmux.pdksh b/pdksh/pdkshrc.d/tmux.pdksh deleted file mode 100644 index f0d0e36a..00000000 --- a/pdksh/pdkshrc.d/tmux.pdksh +++ /dev/null @@ -1,19 +0,0 @@ -# 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 (($#)) ; 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 "$@" -} @@ -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/bash/bashrc.d/bc.bash b/sh/shrc.d/bc.sh index 643678ac..643678ac 100644 --- a/bash/bashrc.d/bc.bash +++ b/sh/shrc.d/bc.sh diff --git a/bash/bashrc.d/diff.bash b/sh/shrc.d/diff.sh index 2c752c8d..2c752c8d 100644 --- a/bash/bashrc.d/diff.bash +++ b/sh/shrc.d/diff.sh diff --git a/bash/bashrc.d/ed.bash b/sh/shrc.d/ed.sh index b87894df..c7afb176 100644 --- a/bash/bashrc.d/ed.bash +++ b/sh/shrc.d/ed.sh @@ -5,7 +5,7 @@ ed() { # We're only adding options if input is from a terminal - if [[ -t 0 ]] ; then + if [ -t 0 ] ; then # Colon prompt (POSIX) set -- -p : "$@" @@ -18,7 +18,7 @@ ed() { # Execute the ed(1) call, in a wrapper if appropriate and with the # concluded options - if [[ -t 0 ]] && hash rlwrap 2>/dev/null ; then + if [ -t 0 ] && hash rlwrap 2>/dev/null ; then command rlwrap ed "$@" else command ed "$@" diff --git a/bash/bashrc.d/gdb.bash b/sh/shrc.d/gdb.sh index ec9d4137..ec9d4137 100644 --- a/bash/bashrc.d/gdb.bash +++ b/sh/shrc.d/gdb.sh diff --git a/bash/bashrc.d/gpg.bash b/sh/shrc.d/gpg.sh index 62d123ea..62d123ea 100644 --- a/bash/bashrc.d/gpg.bash +++ b/sh/shrc.d/gpg.sh diff --git a/bash/bashrc.d/hgrep.bash b/sh/shrc.d/hgrep.sh index 2f7f8d54..b6143b37 100644 --- a/bash/bashrc.d/hgrep.bash +++ b/sh/shrc.d/hgrep.sh @@ -4,12 +4,12 @@ # $ history | grep PATTERN # hgrep() { - if ! (($#)) ; then - printf >&2 '%s: Need a pattern\n' "$FUNCNAME" + if [ "$#" -eq 0 ] ; then + printf >&2 'hgrep: Need a pattern\n' exit 2 fi - if ! [[ -n $HISTFILE ]] ; then - printf >&2 '%s: No HISTFILE\n' "$FUNCNAME" + 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/bash/bashrc.d/mkcd.bash b/sh/shrc.d/mkcd.sh index 6342d4a6..6342d4a6 100644 --- a/bash/bashrc.d/mkcd.bash +++ b/sh/shrc.d/mkcd.sh diff --git a/bash/bashrc.d/mysql.bash b/sh/shrc.d/mysql.sh index 0d5ddb86..00b6930c 100644 --- a/bash/bashrc.d/mysql.bash +++ b/sh/shrc.d/mysql.sh @@ -11,11 +11,9 @@ # database=bar # mysql() { - local config - config=$HOME/.mysql/$1.cnf - if [[ -r $config ]] ; then + if [ -f "$HOME"/.mysql/"$1".cnf ] ; then shift - set -- --defaults-extra-file="$config" "$@" + set -- --defaults-extra-file="$HOME"/.mysql/"$1".cnf "$@" fi command mysql "$@" } diff --git a/bash/bashrc.d/pwgen.bash b/sh/shrc.d/pwgen.sh index 7ba056e5..7ba056e5 100644 --- a/bash/bashrc.d/pwgen.bash +++ b/sh/shrc.d/pwgen.sh diff --git a/bash/bashrc.d/rcsdiff.bash b/sh/shrc.d/rcsdiff.sh index 18b1d324..18b1d324 100644 --- a/bash/bashrc.d/rcsdiff.bash +++ b/sh/shrc.d/rcsdiff.sh 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/pdksh/pdkshrc.d/scr.pdksh b/sh/shrc.d/scr.sh index 01bd20cb..255b9322 100644 --- a/pdksh/pdkshrc.d/scr.pdksh +++ b/sh/shrc.d/scr.sh @@ -2,10 +2,5 @@ # 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() { - if (($# <= 1)) ; then - cd -- "$(mktd "${1:-scr}")" - else - printf 'ksh: scr: too many arguments\n' >&2 - return 2 - fi + cd -- "$(mktd "${1:-scr}")" } diff --git a/bash/bashrc.d/sudo.bash b/sh/shrc.d/sudo.sh index d6d91d12..a5883168 100644 --- a/bash/bashrc.d/sudo.bash +++ b/sh/shrc.d/sudo.sh @@ -1,7 +1,5 @@ # Add the -H parameter to sudo(8) calls, always use the target user's $HOME sudo() { - if [[ $1 != -v ]] ; then - set -- -H "$@" - fi + [ "$1" != -v ] && set -- -H "$@" command sudo "$@" } diff --git a/bash/bashrc.d/tmux.bash b/sh/shrc.d/tmux.sh index f0d0e36a..bd954be8 100644 --- a/bash/bashrc.d/tmux.bash +++ b/sh/shrc.d/tmux.sh @@ -2,7 +2,7 @@ tmux() { # If given any arguments, just use them as they are - if (($#)) ; then + if [ "$#" -gt 0 ] ; then : # If a session exists, just attach to it diff --git a/pdksh/pdkshrc.d/vim.pdksh b/sh/shrc.d/vim.sh index 37fc1871..fc04c99f 100644 --- a/pdksh/pdkshrc.d/vim.pdksh +++ b/sh/shrc.d/vim.sh @@ -1,7 +1,5 @@ # If Vim exists on the system, use it instead of ex, vi, and view -if ! hash vim 2>/dev/null ; then - return -fi +command -v vim >/dev/null || return # Define functions proper ex() { |