bash_functions
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| bash_functions [2014/01/21 17:19] – [Check if it is factor] k2patel | bash_functions [2020/08/10 02:35] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 178: | Line 178: | ||
| [[ http:// | [[ 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.1390324764.txt.gz · Last modified: 2020/08/10 02:28 (external edit)
