Skip to content

Instantly share code, notes, and snippets.

@farhaven
Created April 26, 2010 13:53
Show Gist options
  • Save farhaven/379353 to your computer and use it in GitHub Desktop.
Save farhaven/379353 to your computer and use it in GitHub Desktop.
#!/bin/env bash
# example pipe commands:
# 'while read u; firefox $u; done' -- useful to pipe urls into programs that don't handle them from stdin
# the first line of every cache file is the pipe command used for that file
# dmenu defaults
if [ -f ~/.dmenurc ]; then
source ~/.dmenurc
else
DMENU="dmenu"
fi
# commandline argument handling
verbose="false"
pipe="/bin/bash"
for p in "$@"; do
if [ "$p" = "-v" ]; then
verbose="true"
else
pipe="$p"
fi
done
# cache file setup
if [ "x$XDG_CACHE_HOME" = "x" ]; then
cachefile="${HOME}/.cache"
else
cachefile="$XDG_CACHE_HOME"
fi
cachefile="${cachefile}/xrun/$(echo \"$pipe\" | md5sum | cut -d' ' -f1)"
if [ ! -d $(dirname $cachefile) ]; then
mkdir -p $(dirname $cachefile)
fi
if [ ! -f "$cachefile" ]; then
echo "$pipe" > "$cachefile"
fi
# command gathering
cmd=$(tail -n +2 "$cachefile" | $DMENU -l 7 -p "$pipe" )
if [ "x$cmd" = "x" ]; then
exit 0
fi
# command caching
echo "$cmd" >> "$cachefile"
tail -n +2 "$cachefile" | sort | uniq > "$cachefile.$$"
echo "$pipe" > "$cachefile"
cat "$cachefile.$$" >> "$cachefile"
rm "$cachefile.$$"
# command execution
hash $(echo $cmd | cut -d ' ' -f1) 2>/dev/null
if [ $? == 1 ]; then
notify-send -- "not found: $cmd"
fi
if [[ $verbose == "true" ]]; then
output=$(echo "$cmd" | sh -c "$pipe" 2>&1 | sed -e 's|<|\&lt;|g' -e 's|>|\&gt;|g' )
[ "$output" != "" ] && notify-send -- "$output"
else
echo "$cmd" | sh -c "$pipe"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment