shell-base/load/01-basic

94 lines
2.0 KiB
Plaintext
Raw Normal View History

#!/bin/bash
if [[ ! $(command -v arch) ]]; then
function arch() {
uname -m
}
fi
function function_list {
if [ $1 ]; then
# will list functions in passed file
local STR
local func
declare -a FUNCS
STR=$(grep '() {\|(){\|function' $1)
readarray -t LIST <<<"$STR"
for func in "${LIST[@]}"
do
if [[ ! $func =~ ^# ]]; then
func=${func#function }
func=${func%()?( ){}
[[ ! $func =~ ^_ ]] && FUNCS+=($func)
fi
done
echo "${FUNCS[@]}"
else
# will list all sourced/declared functions available
echo
echo -e "\033[1;4;32m""Functions:""\033[0;34m"
declare -F | grep -v "declare -f\s_" | awk {'print $3'}
echo
echo -e "\033[0m"
fi
}
function alias_list() {
echo
echo -e "\033[1;4;32m""Aliases:""\033[0;34m"
alias | awk {'print $2'} | awk -F= {'print $1'}
echo
echo -e "\033[0m"
}
get_shopt () {
echo $([ "$(shopt -p $1 | grep '\-s')" ] && echo on || echo off)
}
# edit files
# echo set the system editor using EDITOR environment variable
edit() { ${EDITOR:-nano} $@; }
gedit() { ${GEDITOR:-xed} $@; }
unlocku () ( faillock --user $1 --reset )
rsynchg() { rsync --help | grep "\-$1"; }
2024-04-29 13:01:28 -07:00
gip () {
2024-04-29 13:43:56 -07:00
if which jq &> /dev/null; then
2024-04-29 14:19:52 -07:00
curl -s "https://dns.google/resolve?name=$1" | jq -r '.Answer[] | .data' 2>/dev/null | tail -n 1
2024-04-29 13:01:28 -07:00
return 0
else
echo install jq to use this function
return 1
fi
2024-04-29 13:05:43 -07:00
}
gpip () {
curl https://ipv4.nsupdate.info/myip
2024-04-29 13:30:47 -07:00
}
2024-04-29 14:16:07 -07:00
valid_ipv4() {
local ip="$1"
[[ "$ip" =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]] || return 1
for i in ${ip//./ }; do
[[ "${#i}" -gt 1 && "${i:0:1}" == 0 ]] && return 1
[[ "$i" -gt 255 ]] && return 1
done
return 0
2024-04-29 13:30:47 -07:00
}
2024-04-29 14:16:07 -07:00
dip () {
local ip4
ip4=$(/bin/drill $1 | grep -A1 'ANSWER SECTION' | grep -v "ANSWER SECTION" | rev | cut -f1 | rev)
# echo first: $ip4
if valid_ipv4 $ip4; then
echo $ip4
else
/bin/drill $ip4 | grep -A1 'ANSWER SECTION' | grep -v "ANSWER SECTION" | rev | cut -f1 | rev
fi
}