Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Open a file by association. -o to open -e to edit.
#!/bin/bash
#long file extension usually means it's a text
SELF="$(cd ${0%/*} && echo $PWD/${0##*/})"
if [[ $1 == "-o" ]]; then
a="$HOME/.association"
text="vim -u /usr/share/vim/vim73/macros/less.vim"
default="xdg-open"
elif [[ $1 == "-e" ]]; then
a="$HOME/.associationedit"
text="vim"
default="$SELF -o"
fi
f=$2
ext=`basename "$2" | awk -F"." '{ print $NF }'`
regexurl='^(http|https|ftp)\://'
if [[ $2 =~ $regexurl ]]; then
f=`mktemp`
mv $f $f.$ext
f="$f.$ext"
wget -q -O $f "$2"
fi
p=`cat $a | grep -iE -B 1 "(^$ext | $ext | $ext$|^$ext$)" | head -n 1`
l=`echo -e "$p\c" | wc -m`
if [[ $l -gt 0 ]]; then
$p "$f"
else
l=`echo -e "$ext\c" | wc -m`
if [[ $l -gt 4 ]]; then
$text "$f"
else
$default "$f"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment