Skip to content

Instantly share code, notes, and snippets.

@isaacl
Created November 16, 2014 00:55
Show Gist options
  • Save isaacl/84471281361e5c99527a to your computer and use it in GitHub Desktop.
Save isaacl/84471281361e5c99527a to your computer and use it in GitHub Desktop.
A better version of 'which' which understands builtins and follows symlinks
#!/bin/bash -e
com="$1"
if [[ -z "$com" ]]; then
exit 1
fi
while true; do
case $(type -t "$com") in
function)
type f | tail -n+2 | vimcat -c 'set syntax=sh'
exit
;;
alias)
com="$(alias "$com" | cut -d"'" -f2)"
echo "alias: -> '$com'"
;;
builtin)
echo "Shell builtin."
exit
;;
file)
com="$(type -p "$com")"
COUNTER=20
while [ $COUNTER -gt 0 ]; do
if [ -h "$com" ]; then
com="$(readlink "$com")"
echo "sym: -> '$com'"
elif [ -e "$com" ]; then
filet="$(file "$com")"
if [[ $filet == *text* ]]; then
if [[ $(wc -l "$com" | awk '{print $1}') -lt 20 ]]; then
vimcat "$com"
else
vimpager "$com"
fi
echo "'$com'"
else
ls -l "$com"
echo "$filet"
fi
exit
else
echo "<file missing>"
exit 1
fi
(( COUNTER -= 1 ))
done
exit 1
;;
*)
exit 1
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment