This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
bash_functions [2013/10/28 16:34] k2patel [Check if it is factor] |
bash_functions [2020/08/10 02:35] (current) |
||
---|---|---|---|
Line 161: | Line 161: | ||
[[ http://www.linkedin.com/groups/Fridays-functon-dec2bin-Convert-decimal-3716796.S.277082108 | From Article ]] | [[ http://www.linkedin.com/groups/Fridays-functon-dec2bin-Convert-decimal-3716796.S.277082108 | From Article ]] | ||
+ | |||
+ | ==== 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't show its own process | ||
+ | ss=\[${ss:0:1}\]${ss#?} | ||
+ | ps -ef | grep "$ss" | ||
+ | [ $# -gt 1 ] && echo | ||
+ | done | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | [[ http://www.linkedin.com/groups/Fridays-function-psg-Show-all-3716796.S.5827514953933824001?view=&gid=3716796&type=member&item=5827514953933824001&trk=eml-anet_dig-b_nd-pst_ttle-cn | From Article ]] | ||
+ | |||
+ | ==== Print the result of arithmetical calulation ==== | ||
+ | |||
+ | <code bash> | ||
+ | calc() #@ Print the result of arithmetical calulation | ||
+ | { #@ USAGE: calc EXPRESSION | ||
+ | awk 'BEGIN {print '"${*//x/*}"'}' | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | [[ http://www.linkedin.com/groups/Fridays-function-calc-Print-result-3716796.S.5832542139925430274?view=&srchtype=discussedNews&gid=3716796&item=5832542139925430274&type=member&trk=eml-anet_dig-b_pd-ttl-cn&fromEmail=&ut=04XvycE_RsJm41 | From Article ]] | ||
+ | |||
+ | ==== 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=$'\e[s' cu_restore=$'\e[u' cu_down=$'\e[B' | ||
+ | printf "$cu_restore$cu_down$cu_save%s" "$@" | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | [[ https://www.linkedin.com/groups/Fridays-function-lines-Print-lines-3716796.S.5900869707560415236?view=&item=5900869707560415236&type=member&gid=3716796&trk=eml-b2_anet_digest-hero-1-hero-disc-disc-0&midToken=AQG8JYIVdmXjvA&fromEmail=fromEmail&ut=0fjZG80YW_UCk1 | From Article ]] | ||
+ | |||
+ | ==== Integer or Decimal ==== | ||
+ | |||
+ | <code bash> | ||
+ | case ${1#-} in | ||
+ | *[!0-9.]*|.) echo not an integer or float ;; | ||
+ | *.*) echo float ;; | ||
+ | *) echo int ;; | ||
+ | esac | ||
+ | </code> | ||
+ | - OR - | ||
+ | <code bash> | ||
+ | i=$1 | ||
+ | let 'i++' | ||
+ | |||
+ | if [ $? -eq 0 ] # check in not error returned | ||
+ | then | ||
+ | echo "integer" | ||
+ | else | ||
+ | echo "not integer" | ||
+ | fi | ||
+ | </code> | ||
+ | |||
+ | [[ https://www.linkedin.com/groups/How-can-i-verify-if-3716796.S.5862974732210556931?view=&item=5862974732210556931&type=member&gid=3716796&trk=eml-b2_anet_digest-null-2-null&fromEmail=fromEmail&ut=3KLlfwU1D4UCk1 | From Article ]] | ||
+ | |||
+ | ==== 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 | ||
+ | -*) [ "$_neg" = '-' ] && _neg= || _neg='-' | ||
+ | _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 | ||
+ | ""|0) _n=0 | ||
+ | _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 "fpmul: overflow error: %s\n" "$_tot" >&2 | ||
+ | return 1 | ||
+ | ;; | ||
+ | esac | ||
+ | done | ||
+ | |||
+ | while [ ${#_tot} -lt ${#_places} ] | ||
+ | do | ||
+ | _tot=0$_tot | ||
+ | done | ||
+ | |||
+ | _df= | ||
+ | while [ ${#_df} -lt ${#_places} ] | ||
+ | do | ||
+ | left=${_tot%?} | ||
+ | _df=${_tot#$left}$_df | ||
+ | _tot=$left | ||
+ | done | ||
+ | _fpmul=$_tot${_df:+.$_df} | ||
+ | |||
+ | ## remove trailing zeroes or decimal points | ||
+ | while : | ||
+ | do | ||
+ | case $_fpmul in | ||
+ | *.*[0\ ]|*.) _fpmul=${_fpmul%?} ;; | ||
+ | .*) _fpmul=0$_fpmul ;; | ||
+ | *) break ;; | ||
+ | esac | ||
+ | done | ||
+ | |||
+ | [[ $_var ]] && | ||
+ | printf -v "$_var" %s "$_fpmul" || | ||
+ | printf '%s\n' "$_fpmul" | ||
+ | } | ||
+ | num.dec | ||
+ | </code> | ||
+ | |||
+ | [[http://www.linkedin.com/groups/Fridays-function-fpmul-Perform-floatingpoint-3716796.S.5921339472191987716?view=&item=5921339472191987716&type=member&gid=3716796&trk=eml-b2_anet_digest-hero-1-hero-disc-disc-0&midToken=AQG8JYIVdmXjvA&fromEmail=fromEmail&ut=3_RJ7vAOch56s1|From article]] | ||
+ | |||
+ | ==== 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 '%s\n' "${array[@]}" | ||
+ | done | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | [[https://www.linkedin.com/groups/Fridays-function-ashow-Display-contents-3716796.S.5918816399794409474?view=&item=5918816399794409474&type=member&gid=3716796&trk=eml-b2_anet_digest-hero-4-hero-disc-disc-0&midToken=AQG8JYIVdmXjvA&fromEmail=fromEmail&ut=19mP4Lva0K56s1|From Article]] | ||
+ | |||
+ | ==== 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 <<< "$_word" | ||
+ | [[ ${_var^^} =~ ^[A-Z_][A-Z0-9_]*$ ]] && | ||
+ | printf -v "$_var" %s "$_rw" || | ||
+ | printf '%s\n' "$_rw" | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | [[ https://www.linkedin.com/groups/Fridays-function-revword-Reverse-order-3716796.S.5916260779903975428?view=&item=5916260779903975428&type=member&gid=3716796&trk=eml-b2_anet_digest-hero-1-hero-disc-disc-0&midToken=AQG8JYIVdmXjvA&fromEmail=fromEmail&ut=13sqjyjg1356s1 | From Article ]] | ||
+ | |||
+ | ==== 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##*[!$_char]} | ||
+ | _trim=${_trim%"$_trim_string"} | ||
+ | _trim_string=${_trim%%[!$_char]*} | ||
+ | _trim=${_trim#"$_trim_string"} | ||
+ | is_var "$_var" && | ||
+ | printf -v "$_var" %s "$_trim" || | ||
+ | printf '%s\n' "$_trim" | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | [[ https://www.linkedin.com/groups/Fridays-function-trim-Trim-spaces-3716796.S.5908643378429460481?view=&item=5908643378429460481&type=member&gid=3716796&trk=eml-b2_anet_digest-hero-1-hero-disc-disc-0&midToken=AQG8JYIVdmXjvA&fromEmail=fromEmail&ut=2lgJiLIHdb56s1 | From Article ]] |