This repository has been archived on 2022-02-20. You can view files and clone it, but cannot push or open issues/pull-requests.
bash-shell-base/function/01-basic

89 lines
1.9 KiB
Plaintext
Raw Permalink Normal View History

#!/bin/bash
# 00 will get loaded first
##-- functions which may be used by any of the alias file --#
group_add() {
[[ ! $(getent group $1) ]] && echo "no group $1, aborting" && return 1
echo adding group $1 to $USER
sudo usermod -a -G $1 $USER
}
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 -
}