bash_functions
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| bash_functions [2013/06/28 17:25] – [Print all argument in reverse order] k2patel | bash_functions [2020/08/10 02:35] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 68: | Line 68: | ||
| </ | </ | ||
| [[ http:// | [[ http:// | ||
| + | |||
| + | ==== Add directory or directories to PATH ==== | ||
| + | <code bash> | ||
| + | addpath() #@ add directory or directories to $PATH | ||
| + | { #@ USAGE: addpath DIR ... | ||
| + | local prefix=0 quiet=0 OPTIND=1 p | ||
| + | |||
| + | while getopts iq var | ||
| + | do | ||
| + | case " | ||
| + | i) prefix=1 ;; | ||
| + | q) quiet=1 ;; | ||
| + | esac | ||
| + | done | ||
| + | shift $(( $OPTIND - 1 )) | ||
| + | |||
| + | for p | ||
| + | do | ||
| + | p=${p%" | ||
| + | case $p in | ||
| + | "" | ||
| + | esac | ||
| + | case :$PATH: in | ||
| + | *:$p:*) | ||
| + | (( quiet == 0 )) && echo " | ||
| + | continue | ||
| + | ;; | ||
| + | esac | ||
| + | if [ -d " | ||
| + | then | ||
| + | (( prefix == 1 )) && PATH=$p: | ||
| + | else | ||
| + | [ $quiet -eq 0 ] && echo " | ||
| + | fi | ||
| + | done | ||
| + | export PATH ## probably unnecessary | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | [[ http:// | ||
| + | |||
| + | ==== Return random date in given range of years ==== | ||
| + | <code bash> | ||
| + | random_date() #@ Return random date in given range of years | ||
| + | { #@ USAGE: random_date FIRST LAST [VAR] | ||
| + | #@ Global variables set: random_date [VAR] | ||
| + | local first=${1: | ||
| + | declare -i year month day dim | ||
| + | year=$(( RANDOM % (last-first) + first )) | ||
| + | month=$(( RANDOM % 12 + 1 )) | ||
| + | days_in_month dim " | ||
| + | day=$(( RANDOM % dim + 1 )) | ||
| + | printf -v random_date " | ||
| + | is_var " | ||
| + | printf -v " | ||
| + | printf " | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | [[ http:// | ||
| + | |||
| + | ==== Check if it is factor ==== | ||
| + | <code bash> | ||
| + | is_factor() #@ Return successfully if $2 is a factor of $1 | ||
| + | { #@ USAGE: is_factor NUMBER FACTOR | ||
| + | local number=$1 factor=$2 | ||
| + | (( ((number / factor) * factor) == number )) | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | [[ http:// | ||
| + | |||
| + | ==== Check if it is factor ==== | ||
| + | <code bash> | ||
| + | dec2bin() #@ Convert decimal integer to binary optionally padding with zeroes | ||
| + | { #@ USAGE: dec2bin INT [WIDTH] [VAR] | ||
| + | local val=$1 _w=$2 var=$3 _b=2 _r bin | ||
| + | if (( $# == 1 )) || is_var " | ||
| + | then | ||
| + | _w=1 | ||
| + | fi | ||
| + | while ((val)) | ||
| + | do | ||
| + | _r=$(( $val % $_b )) | ||
| + | bin=$_r$bin; | ||
| + | val=$(( val / _b )) | ||
| + | done | ||
| + | printf -v bin " | ||
| + | is_var " | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | [[ http:// | ||
| + | |||
| + | ==== List running process matching args ==== | ||
| + | <code bash> | ||
| + | psg() #@ Show all running processes matching ARG[s] | ||
| + | { #@ USAGE: psg STRING ... | ||
| + | local ss | ||
| + | for ss | ||
| + | do | ||
| + | ## Enclose first character in brackets so grep doesn' | ||
| + | ss=\[${ss: | ||
| + | ps -ef | grep " | ||
| + | [ $# -gt 1 ] && echo | ||
| + | done | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | [[ http:// | ||
| + | |||
| + | ==== Print the result of arithmetical calulation ==== | ||
| + | |||
| + | <code bash> | ||
| + | calc() #@ Print the result of arithmetical calulation | ||
| + | { #@ USAGE: calc EXPRESSION | ||
| + | awk 'BEGIN {print '" | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | [[ http:// | ||
| + | |||
| + | ==== Print lines on screen descending from the current point ==== | ||
| + | |||
| + | <code bash> | ||
| + | lines() #@ Print lines on screen descending from the current point | ||
| + | { #@ USAGE: [printat ROW COL [STRING];] lines STRING ... | ||
| + | local cu_save=$' | ||
| + | printf " | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | [[ https:// | ||
| + | |||
| + | ==== Integer or Decimal ==== | ||
| + | |||
| + | <code bash> | ||
| + | case ${1#-} in | ||
| + | *[!0-9.]*|.) echo not an integer or float ;; | ||
| + | *.*) echo float ;; | ||
| + | *) echo int ;; | ||
| + | esac | ||
| + | </ | ||
| + | - OR - | ||
| + | <code bash> | ||
| + | i=$1 | ||
| + | let ' | ||
| + | |||
| + | if [ $? -eq 0 ] # check in not error returned | ||
| + | then | ||
| + | echo " | ||
| + | else | ||
| + | echo "not integer" | ||
| + | fi | ||
| + | </ | ||
| + | |||
| + | [[ https:// | ||
| + | |||
| + | ==== floating point multiplication ==== | ||
| + | <code bash> | ||
| + | fpmul() #@ Perform floating-point multiplication in the shell | ||
| + | { #@ USAGE: fpmul [VAR] NUM.DEC ... | ||
| + | local _var= _places _tot _neg _n _int _dec _df _fpmul | ||
| + | if [[ ${1^^} =~ ^[A-Z_][A-Z0-9_]*$ ]] | ||
| + | then | ||
| + | _var=$1 | ||
| + | shift | ||
| + | fi | ||
| + | _places= | ||
| + | _tot=1 | ||
| + | _neg= | ||
| + | for _n | ||
| + | do | ||
| + | ## 2 negatives make a positive | ||
| + | case $_n in | ||
| + | -*) [ " | ||
| + | _n=${_n#-} | ||
| + | ;; | ||
| + | esac | ||
| + | |||
| + | ## Check for non-numeric characters | ||
| + | case $_n in | ||
| + | *[!0-9.]*) return 1 ;; | ||
| + | esac | ||
| + | |||
| + | ## count the number of decimal _places, | ||
| + | ## then remove the decimal point | ||
| + | case $_n in | ||
| + | .*) _int= | ||
| + | _dec=${_n#? | ||
| + | _places=$_places$_dec | ||
| + | _n=$_dec | ||
| + | ;; | ||
| + | *.*) _dec=${_n# | ||
| + | _int=${_n%.*} | ||
| + | _places=$_places$_dec | ||
| + | _n=$_int$_dec | ||
| + | ;; | ||
| + | esac | ||
| + | |||
| + | ## remove leading zeroes | ||
| + | while : | ||
| + | do | ||
| + | case $_n in | ||
| + | "" | ||
| + | _fpmul=0 | ||
| + | return | ||
| + | ;; | ||
| + | 0*) _n=${_n#0} ;; | ||
| + | *) break;; | ||
| + | esac | ||
| + | done | ||
| + | |||
| + | ## multiply by the previous _total | ||
| + | _tot=$(( $_tot * ${_n:-0} )) | ||
| + | |||
| + | ## report any overflow error | ||
| + | case $_tot in | ||
| + | -*) printf " | ||
| + | return 1 | ||
| + | ;; | ||
| + | esac | ||
| + | done | ||
| + | |||
| + | while [ ${#_tot} -lt ${#_places} ] | ||
| + | do | ||
| + | _tot=0$_tot | ||
| + | done | ||
| + | |||
| + | _df= | ||
| + | while [ ${#_df} -lt ${#_places} ] | ||
| + | do | ||
| + | left=${_tot%? | ||
| + | _df=${_tot# | ||
| + | _tot=$left | ||
| + | done | ||
| + | _fpmul=$_tot${_df: | ||
| + | |||
| + | ## remove trailing zeroes or decimal points | ||
| + | while : | ||
| + | do | ||
| + | case $_fpmul in | ||
| + | *.*[0\ ]|*.) _fpmul=${_fpmul%? | ||
| + | .*) _fpmul=0$_fpmul ;; | ||
| + | *) break ;; | ||
| + | esac | ||
| + | done | ||
| + | |||
| + | [[ $_var ]] && | ||
| + | printf -v " | ||
| + | printf ' | ||
| + | } | ||
| + | num.dec | ||
| + | </ | ||
| + | |||
| + | [[http:// | ||
| + | |||
| + | ==== show array ==== | ||
| + | <code bash> | ||
| + | ashow() #@ Display contents of array[s] | ||
| + | { #@ USAGE: ashow ARRAYNAME ... | ||
| + | local _arg _num=0 | ||
| + | for arg | ||
| + | do | ||
| + | :; ((_num++)) && echo | ||
| + | :; declare -n array=$arg | ||
| + | :; [[ -n ${array[*]} ]] && printf ' | ||
| + | done | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | [[https:// | ||
| + | |||
| + | ==== Reverse Word ==== | ||
| + | <code bash> | ||
| + | revword() #@ Reverse the order of characters in STRING | ||
| + | { #@ USAGE: revword STRING [VAR] | ||
| + | local _n _word=$1 _var=$2 _len _r _rw= _l | ||
| + | while read -n1 _l | ||
| + | do | ||
| + | _rw=$_l$_rw | ||
| + | done <<< | ||
| + | [[ ${_var^^} =~ ^[A-Z_][A-Z0-9_]*$ ]] && | ||
| + | printf -v " | ||
| + | printf ' | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | [[ https:// | ||
| + | |||
| + | ==== Trim Spaces ==== | ||
| + | <code bash> | ||
| + | trim() #@ Trim spaces (or char in $2) from both ends of $1 | ||
| + | { #@ USAGE: trim STRING [CHAR [VAR]] | ||
| + | local _trim_string _trim=$1 _char=${2:- } _var=$3 | ||
| + | _trim_string=${_trim## | ||
| + | _trim=${_trim%" | ||
| + | _trim_string=${_trim%%[!$_char]*} | ||
| + | _trim=${_trim#" | ||
| + | is_var " | ||
| + | printf -v " | ||
| + | printf ' | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | [[ https:// | ||
bash_functions.1372440356.txt.gz · Last modified: 2020/08/10 02:28 (external edit)
