Files
linux-workspace/rc/01-functions.sh
2022-05-16 21:02:58 +02:00

93 lines
1.3 KiB
Bash

# $LWS/rc/functions.sh
function xlog() {
if (( $LWS_DEBUG )); then
echo "$@"
fi
}
function jql() {
jq -C . "$@" | less -r
}
function ffile() {
local filename="$1"
shift
file $(type -p "$filename") $@
}
# print time in seconds with nanoseconds precission
function utime() {
date '+%s.%N'
}
function in_path() {
[[ "$PATH" =~ (^|:)"${1}"(:|$) ]]
}
function append_path() {
if ! in_path "$1"; then
PATH="$PATH:$1"
fi
}
function append_path_try() {
if [[ -e "$1" ]]; then
append_pat "$1"
fi
}
function prepend_path() {
if ! in_path "$1"; then
PATH="$1:$PATH"
fi
}
function prepend_path_try() {
if [[ -e "$1" ]]; then
prepend_path "$1"
fi
}
function uniq_path() {
echo "$PATH" | tr ":" "\n" | awk '!seen[$0]++' | tr "\n" ":" | sed -r 's/:+/:/g' | sed -r 's/^:|:$//g'
}
function set_uniq_path() {
PATH="$(uniq_path)"
}
function is_alias() {
alias "$1" > /dev/null 2>&1
}
function can_run() {
local application="$1"
command -v "$application" > /dev/null
}
function source_directory() {
local mask="$1"
for file in $mask; do
if [[ -f "$file" ]]; then
xlog "LOAD $file"
source "$file"
fi
done
}
# use: dump_args "$@"
function dump_args() {
for i in $*; do
echo $i
done
}
function source_try() {
local script="$1"
if [[ -e "$script" ]]; then
source "$script"
fi
}