Skip to content

Instantly share code, notes, and snippets.

@dacap
Last active September 17, 2020 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dacap/a7bc51803032e2f84f181df3a473a094 to your computer and use it in GitHub Desktop.
Save dacap/a7bc51803032e2f84f181df3a473a094 to your computer and use it in GitHub Desktop.
open/close commands // or how to never write "xdg-" or "kill -9" again
#! /bin/bash
# open file/folder/url
if [[ "$(uname)" =~ "Darwin" ]] ; then
true # do nothing because macOS already have "open" command
elif [[ "$(uname)" == "Linux" ]] ; then
alias open=xdg-open
else
alias open=start
fi
# close processname
function close {
pids=$(ps -e | grep $1)
if [[ "$pids" != "" ]] ; then
while IFS= read -r line; do
pid=$(echo $line | tr -s ' ' | cut -d ' ' -f 2)
cmd=$(echo $line | tr -s ' ' | cut -d ' ' -f 5)
if [[ "$cmd" == "$1" ]] ; then
echo "Closing $cmd ($pid)"
kill -9 $pid
fi
done <<< "$pids"
else
echo Process $1 not found
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment