This repository has been archived on 2022-02-20. You can view files and clone it, but cannot push or open issues or pull requests.
bash-shell-base/function/01-basic
David Kebler 0ac5906eee refactor the way the module library is loaded
added startup script callable from profile.d
renamed setup.sh to load.sh
added rsync copy function cprs
added shell.env that is called by /etc/profile to set up access to this  and related repos
2020-11-13 10:25:04 -08:00

70 lines
1.6 KiB
Bash

#!/bin/bash
# 00 will get loaded first
##-- functions which may be used by any of the alias file --#
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; }