From 74c06de77d0ee3ea428a9fa0f6a83274ef62930f Mon Sep 17 00:00:00 2001 From: Iain Patterson Date: Fri, 24 Apr 2009 17:49:41 +0100 Subject: [PATCH] Use find_working to prepare aliases. The find_working script will find a usable program somewhere in the PATH. --- .profile.d/aliases.bashrc | 10 +++---- .profile.d/screen.bashrc | 10 ++++--- .profile.d/vim.bashrc | 3 ++ opt/bin/find_working | 61 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 75 insertions(+), 9 deletions(-) create mode 100644 .profile.d/vim.bashrc create mode 100755 opt/bin/find_working diff --git a/.profile.d/aliases.bashrc b/.profile.d/aliases.bashrc index 943f879..18a8b76 100644 --- a/.profile.d/aliases.bashrc +++ b/.profile.d/aliases.bashrc @@ -1,7 +1,7 @@ -alias knh='kill_known_host' -if which vim 2>/dev/null | grep ^/ >/dev/null; then - alias vim='vim +syn\ on' - alias vi='vim' -fi +unalias rm 2>/dev/null +unalias mv 2>/dev/null +unalias cp 2>/dev/null + alias debug='valgrind --tool=memcheck --leak-check=yes --show-reachable=yes --num-callers=5 --verbose --time-stamp=yes --track-fds=yes' +alias knh='kill_known_host' alias svnfind='find . -type f ! -path "*/.svn/*"' diff --git a/.profile.d/screen.bashrc b/.profile.d/screen.bashrc index 90b3707..ca08f6e 100644 --- a/.profile.d/screen.bashrc +++ b/.profile.d/screen.bashrc @@ -1,16 +1,18 @@ # Fixup screen and define ``session'' if available. -screen=$(which screen 2>/dev/null) -if [ ! -z "$screen" ]; then +screen=$(find_working screen 2>/dev/null) +if [ $? = 0 ]; then # Override SCREENDIR iff screen is not setuid. if ! ls -l "$screen" | cut -d ' ' -f 1 | grep s >/dev/null; then export SCREENDIR="$HOME/.screen/$HOSTNAME" fi - if which krenew 2>/dev/null | grep ^/ >/dev/null; then - alias kscreen='krenew --' + krenew=$(find_working krenew 2/dev/null) + if [ $? = 0 ]; then + alias kscreen="$krenew --" else alias kscreen='' fi + unset krenew alias session='bigscreen -rx -S session || kscreen bigscreen -S session' fi diff --git a/.profile.d/vim.bashrc b/.profile.d/vim.bashrc new file mode 100644 index 0000000..4ee1bc0 --- /dev/null +++ b/.profile.d/vim.bashrc @@ -0,0 +1,3 @@ +vim=$(find_working vim 2>/dev/null) +[ $? = 0 ] && alias vi="$vim" +unset vim diff --git a/opt/bin/find_working b/opt/bin/find_working new file mode 100755 index 0000000..cbbd82d --- /dev/null +++ b/opt/bin/find_working @@ -0,0 +1,61 @@ +#!/bin/bash +# +# find_working: Find a version of some tool in the PATH which actually runs. +# Usage: find_working [options] +# Options: -a Use arguments to test executable files. Default --help. +# -q Don't print path to prog. Just exit 0 if found. +# -x Don't try to execute unreadable files. Assume success. +# -X Don't try to execute unreadable files. Assume failure. +# + +args="-h -? --help" +quiet=0 +unreadable= +while getopts ":a:qxX" opt; do + case $opt in + a) args="$OPTARG";; + q) quiet=1;; + x) unreadable=0;; + X) unreadable=1;; + esac +done +shift $((OPTIND-1)) + +prog="$1"; shift +if [ -z "$prog" ]; then + echo >&2 "Usage: find_working [options] " + echo >&2 "Options: -a Use arguments to test executable files. Default --help." + echo >&2 " -q Don't print path to prog. Just exit 0 if found." + echo >&2 " -x Don't try to execute unreadable files. Assume success." + echo >&2 " -X Don't try to execute unreadable files. Assume failure." + exit 1 +fi + +ret= +for path in ${PATH//:/ }; do + [ -x "$path/$prog" ] || continue + + if [ -r "$path/$prog" ]; then + ldd "$path/$prog" 2>/dev/null | grep "not found" >/dev/null && continue + ret="$path/$prog" + break + elif [ -z "$unreadable" ]; then + "$path/$prog" $args 2>&1 | grep " $prog " >/dev/null || continue + ret="$path/$prog" + break + elif [ $unreadable = 0 ]; then + ret="$path/$prog" + break + elif [ $unreadable = 1 ]; then + continue + fi + + exit 0 +done + +if [ -n "$ret" ]; then + [ $quiet = 0 ] && echo "$ret" + exit 0 +fi + +exit 100 -- 2.20.1