# Execute a commad in background and return its pid
start(){
"$@" &
pid=$!
clean pre "kill $pid"
return $pid
}
# Clean exit #
# Record command lines passed as argument and execute them all when called without args #
# One argument = One command #
# TODO append or prepend according to arg
declare -a driglibash_clean_actions
clean() {
if [ "$#" -eq 0 ] ; then
echo "Cleaning"
for action in "${driglibash_clean_actions[@]}" ; do
echo "driglibash_clean> $action"
$action
done
elif [ "$#" -eq 1 ] ; then
driglibash_clean_actions+=("$1")
elif [ "$#" -eq 2 ] ; then
case "$1" in
"pre")
declare -a tmp
tmp=("${driglibash_clean_actions[@]}")
driglibash_clean_actions=("$2")
driglibash_clean_actions+=("${tmp[@]}")
;;
"post")
driglibash_clean_actions+=("$2")
;;
"del")
for i in "${!driglibash_clean_actions[@]}" ; do
if [ "$2" = "${driglibash_clean_actions[$i]}" ] ; then
unset driglibash_clean_actions[$i]
break
fi
done
;;
*)
die "driglibash_clean: action '$1' not supported"
esac
else
die "driglibash_clean : Bad clean usage, receveid more than two args"
fi
}
# tells where your executable is (absolute path). Follow simlinks if any argument provided
where() {
if [ -z "$1" ] ; then
echo "$( cd -P "$( dirname "$1" )" && pwd )"
else
SOURCE="$0"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
SOURCE="$(readlink "$SOURCE")"
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
echo $DIR
fi
}
# Add the line $1 in file $2 if not present
line_in_file() {
if [ "$#" -ne 2 ] ; then die "Bad usage of 'line_in_file'. Got '$#' parameters: '$@'" ; fi
if [ -z "$1" ] ; then die "Line arg is emtpy in 'line_in_file'" ; fi
line="$1"
if [ -z "$2" ] ; then die "File arg is emtpy in 'line_in_file'" ; fi