Skip to content

Instantly share code, notes, and snippets.

@dacap

dacap/bash_profile.sh

Last active Sep 17, 2020
Embed
What would you like to do?
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