shell-base/functions/01-basic

97 lines
2.0 KiB
Bash

#!/bin/bash
# 00 will get loaded first
##
user_reload() {
save=$PWD
exec su -l $USER
cd $save
}
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"
}
rsynchc() { rsync --help | grep "\-$1"; }
alias rsynch="rsynchc"
get_shopt () {
echo $([ "$(shopt -p $1 | grep '\-s')" ] && echo on || echo off)
}
change-ext() {
local depth
local d
d=${3:-0}
let d+=1
[ $d -lt 1 ] && depth="" || depth=" -maxdepth $d "
find . $depth -name '*.'$1 -exec rename -n 's/\.'$1'/.'$2'/' \{} \;
read -p "do REALLY want to change the extensions? " -n 1 -r
echo
[[ ! $REPLY =~ ^[Yy]$ ]] && return 1
find . $depth -name '*.'$1 -exec rename 's/\.'$1'/.'$2'/' \{} \;
}
alias chext=change-ext
# find in any file
fif() {
grep -rnswl $1 -e $2 | more
}
# edit files
# set the system editor using EDITOR environment variable
editor() { ${EDITOR} $1; }
add-repo-key () {
gpg --keyserver keyserver.ubuntu.com --recv-key $1
gpg -a --export $1 | sudo apt-key add -
}
osinfo () {
echo kernel: $(uname -r)
echo machine: $(arch)
cat /etc/os-release
cat /etc/upstream-release/lsb-release
}
fsudo () # run a function as sudo
{
[[ "$(type -t $1)" == "function" ]] &&
ARGS="$@" && sudo bash -c "$(declare -f $1); $ARGS"
}
alias ssudo="ssudo "