2022-04-06 12:27:26 -07:00
|
|
|
#!/bin/bash
|
2023-01-20 10:09:56 -08:00
|
|
|
|
|
|
|
str_after () {
|
|
|
|
echo "$1" | cut -s -d "${2:-=}" -f2
|
|
|
|
}
|
|
|
|
|
|
|
|
str_before () {
|
|
|
|
echo "$1" | cut -s -d "${2:-=}" -f1
|
|
|
|
}
|
|
|
|
|
2022-04-06 12:27:26 -07:00
|
|
|
# must be json as a string, depends on jq
|
|
|
|
get_prop_value () {
|
|
|
|
local value
|
|
|
|
# echo in $1 get $2
|
|
|
|
value=$(echo $1 | jq -r .$2)
|
|
|
|
echo $value
|
|
|
|
}
|
|
|
|
|
|
|
|
is_array() {
|
|
|
|
local variable_name=$1
|
|
|
|
[[ "$(declare -p $variable_name 2>/dev/null)" =~ "declare -a" ]]
|
|
|
|
}
|
|
|
|
|
2022-12-24 12:34:03 -08:00
|
|
|
is_function() {
|
|
|
|
[[ $(type $1 2> /dev/null) ]] && echo $1 is a function
|
|
|
|
}
|
|
|
|
|
2022-04-06 12:27:26 -07:00
|
|
|
filename() {
|
|
|
|
# passed entire path
|
|
|
|
echo $(basename "$1" | rev | cut -f 2- -d '.' | rev)
|
|
|
|
}
|
|
|
|
|
2022-12-31 23:28:18 -08:00
|
|
|
fileext() {
|
|
|
|
# passed entire path
|
|
|
|
echo $1 | awk -F . '{print $NF}'
|
|
|
|
}
|
|
|
|
|
2022-04-06 12:27:26 -07:00
|
|
|
# // TODO remove and use path module
|
|
|
|
# // must change acl.lib and loginout, chromium, and ungoogled install files
|
|
|
|
adirname() {
|
|
|
|
# passed entire path
|
|
|
|
echo "$(cd "$(dirname "$1")" >/dev/null 2>&1 ; pwd -P )"
|
|
|
|
}
|
|
|
|
|
|
|
|
user_exists() {
|
|
|
|
id -u $1 > /dev/null 2>&1
|
|
|
|
[[ $? == 1 ]] || echo $1
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
chmod_dirs() {
|
|
|
|
# passed entire path
|
|
|
|
local usesudo
|
|
|
|
[[ $1 == -s ]] && usesudo="sudo" && shift 2
|
|
|
|
$usesudo find $1 -type f -exec chmod $2 {} +
|
|
|
|
}
|
|
|
|
|
|
|
|
parse_option () {
|
|
|
|
# usage: parse_option -f "-b one -p 22 -F another" -p
|
|
|
|
# if -f is used then it will return the complete option
|
|
|
|
# otherwise just the options value
|
|
|
|
local opts;local f;local opt; local ret
|
|
|
|
[[ $1 = "-f" ]] && { f=true;shift 1; }
|
2024-02-21 11:11:07 -08:00
|
|
|
[[ ! $# -eq 2 ]] && return 1
|
2022-04-06 12:27:26 -07:00
|
|
|
opts=$1
|
|
|
|
opt=$2
|
2024-02-21 11:11:07 -08:00
|
|
|
# echo $opts, $opt
|
|
|
|
[[ ! $opts =~ $opt ]] && return 1
|
|
|
|
ret=$(sed -n '/^.*'"$opt"'\s\+\(\w\+\).*$/s//\1/p' <<< $opts)
|
2022-04-06 12:27:26 -07:00
|
|
|
[[ $f ]] && echo "$2 $ret" || echo $ret
|
|
|
|
}
|
|
|
|
|
2024-02-21 11:11:07 -08:00
|
|
|
# TODO: need to pass two arrays and then pass them back, see -r ret in ssh for example
|
|
|
|
# parse_extra_args () {
|
|
|
|
# if [[ ! $* =~ "--" ]]; then
|
|
|
|
# debug $( ( IFS=$','; echo all arguments: "$*" ) )
|
|
|
|
# for ((d=1; d<$#; ++d)); do
|
|
|
|
# [[ ${!d} == "--" ]] && break
|
|
|
|
# done
|
|
|
|
# if [[ $d -lt $# ]]; then # if there are extra ssh arguments
|
|
|
|
# debug found -- at $d
|
|
|
|
# opts=("${@:$d+1:$#}")
|
|
|
|
# debug $( ( IFS=$','; echo "ssh arguments: ${opts[*]}" ) )
|
|
|
|
# # [[ ! ${sshargs[0]} ]] && { echo missing remote machine, must provide at least a hostname, -- hostname; return 3; }
|
|
|
|
# ropts=("${@:1:$d-1}")
|
|
|
|
# debug $( ( IFS=$','; echo "remaining arguments to parse: ${ropts[*]}" ) )
|
|
|
|
# fi
|
|
|
|
# fi
|
|
|
|
# }
|
|
|
|
|
2023-01-05 12:36:23 -08:00
|
|
|
remove_empty_lines () {
|
2024-02-16 23:49:08 -08:00
|
|
|
if [[ -f $1 ]]; then cat $1; else echo "$1"; fi | sed -n '/^\s*$/!p'
|
|
|
|
# sed -rz 's/^\n+//; s/\n+$/\n/g'
|
2023-01-05 12:36:23 -08:00
|
|
|
}
|
|
|
|
|
2024-02-22 13:47:44 -08:00
|
|
|
# $(sed 's/ /\\ /g' <<< $i)"
|
|
|
|
escape_char () {
|
|
|
|
echo ${1//$2/\\$2}
|
|
|
|
}
|
|
|
|
|
|
|
|
escape_spaces () {
|
|
|
|
escape_char "$1" " "
|
|
|
|
}
|
|
|
|
|
|
|
|
escape_args () {
|
2024-02-21 11:11:07 -08:00
|
|
|
local rargs
|
2024-02-22 13:47:44 -08:00
|
|
|
for i; do rargs="$rargs $(escape_spaces "$i")"; done
|
2024-02-21 11:11:07 -08:00
|
|
|
echo $rargs
|
|
|
|
}
|
|
|
|
|
2022-04-06 12:27:26 -07:00
|
|
|
remove_end_spaces () {
|
|
|
|
del=${2:-\'}
|
|
|
|
# echo delimiter: $del
|
2024-02-21 11:11:07 -08:00
|
|
|
# sed -e 's/[[:space:]]\{1,\}$del/$del/' <<< "$1"
|
2024-02-24 08:01:41 -08:00
|
|
|
res=$(sed -e 's/^'$del'[[:space:]]*/'$del'/' <<< "$1")
|
|
|
|
sed -e 's/[[:space:]]*'${del}'$/'$del'/' <<< "$res"
|
2022-04-06 12:27:26 -07:00
|
|
|
}
|
|
|
|
|
2023-01-05 12:36:23 -08:00
|
|
|
# pass any sed ' ' string and comments lines will be ignored
|
2022-12-24 12:34:03 -08:00
|
|
|
# https://unix.stackexchange.com/a/301432/201387
|
|
|
|
# https://stackoverflow.com/a/35874420/4695378
|
|
|
|
sed_ignore_comments () {
|
|
|
|
cmd="sed -r 'h;s/[^#]*//1;x;s/#.*//;${1};G;s/(.*)\n/\1/'"
|
|
|
|
if (( $# == 2 )) ; then
|
|
|
|
eval $cmd <<< "$2"
|
|
|
|
else
|
|
|
|
eval $cmd < /dev/stdin
|
|
|
|
fi
|
|
|
|
}
|
2023-01-20 10:09:56 -08:00
|
|
|
|
2024-02-16 23:49:08 -08:00
|
|
|
compact_file () {
|
|
|
|
# removes trailing spaces, blank lines and comments
|
|
|
|
# echo -e "$1"
|
|
|
|
# grep -v '^\s*#' $1
|
|
|
|
[[ -f $1 ]] && sed -i -e '/^\s*#/d' -e '/^\s*$/d' "$1"
|
|
|
|
# if [[ -f "$1" ]]; then cat "$1"; else echo -e "$@"; fi | sed 's/\\s*$//g' | sed -r '/^\s*$/d' | grep -v '^\s*#'
|
|
|
|
}
|
|
|
|
|
2024-02-21 11:11:07 -08:00
|
|
|
mkrfilename () {
|
|
|
|
echo ${2:-/tmp/}${1:-file}.$RANDOM
|
|
|
|
}
|
2024-02-16 23:49:08 -08:00
|
|
|
|
2024-03-05 13:50:09 -08:00
|
|
|
rm_ext () {
|
|
|
|
# sed -re 's/(^.*[^/])\.[^./]*$/\1/' <<< "$1"
|
|
|
|
echo "${1%.*}"
|
|
|
|
}
|
|
|
|
|
|
|
|
get_ext () {
|
|
|
|
echo "${1##*.}"
|
|
|
|
}
|
|
|
|
|
|
|
|
get_fname () {
|
|
|
|
# basename $1
|
|
|
|
echo "${1##*/}"
|
|
|
|
}
|
|
|
|
|
|
|
|
get_only_fname () {
|
|
|
|
rm_ext "$(get_fname "$1")"
|
|
|
|
}
|
|
|
|
|
|
|
|
get_dir () {
|
|
|
|
echo "${1%/*}"
|
|
|
|
}
|
2024-02-16 23:49:08 -08:00
|
|
|
|
2023-12-14 13:59:24 -08:00
|
|
|
#rows between matched rows
|
|
|
|
# sed '1,/firstmatch/d;/secondmatch/,$d'
|
|
|
|
|
2024-02-21 11:11:07 -08:00
|
|
|
# this will escape ' for all lines having sed
|
|
|
|
# sed -i '/sed/s/\x27/\\'"'"'/g' $tmp_file
|