This shows you the differences between two versions of the page.
bash_functions [2013/10/28 16:31] k2patel [Return random date in given range of years] |
bash_functions [2020/08/10 02:35] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Bash Functions ====== | ||
- | ==== index # Print the position of $2 in $1 ==== | ||
- | <code bash> | ||
- | index() #@ Print the position of $2 in $1 | ||
- | { #@ USAGE: index STRING SUBSTRING | ||
- | local idx | ||
- | case $1 in | ||
- | *$2*) idx=${1%%$2*} | ||
- | echo $(( ${#idx} + 1 )) ;; | ||
- | *) echo 0 ;; | ||
- | esac | ||
- | } | ||
- | </code> | ||
- | [[http://www.linkedin.com/groups/Fridays-function-index-3716796.S.237745984?view=&srchtype=discussedNews&gid=3716796&item=237745984&type=member&trk=eml-anet_dig-b_pd-ttl-cn&ut=0kcr73WwfdW5I1 | From Article]] | ||
- | ==== die # Exit with error code and optional message ==== | ||
- | <code bash> | ||
- | die() #@ Exit with error code and optional message | ||
- | { #@ USAGE: die RETURN_CODE [MESSAGE] | ||
- | result=$1 | ||
- | shift | ||
- | [ -n "$*" ] && printf "%s\n" "$*" >&2 | ||
- | exit "$result" | ||
- | } | ||
- | </code> | ||
- | [[ http://www.linkedin.com/groups/Fridays-function-die-3716796.S.236067929?qid=da8e8fcc-4fda-480a-91a1-3ef360d90e53&trk=group_most_popular_guest-0-b-ttl&goback=.gde_3716796_member_237745984.gmp_3716796 | From Article ]] | ||
- | |||
- | |||
- | ==== Place SUBSTRING over STRING beginning at OFFSET ==== | ||
- | <code bash> | ||
- | overlay() #@ Place SUBSTRING over STRING beginning at OFFSET | ||
- | { #@ USAGE: overlay VARNAME SUBSTRING OFFSET | ||
- | local varname=$1 substr=$2 start=$3 string | ||
- | eval "string=\${$varname}" | ||
- | left=${string:0:start} | ||
- | right=${string:start+${#substr}} | ||
- | printf -v "$varname" %s%s%s "$left" "$substr" "$right" | ||
- | } | ||
- | </code> | ||
- | [[ http://www.linkedin.com/groups/Fridays-function-overlay-3716796.S.241936021?view=&gid=3716796&type=member&item=241936021&trk=eml-anet_dig-b_nd-pst_ttle-cn | From Article ]] | ||
- | |||
- | ==== Print the ASCII value of the first character ==== | ||
- | <code bash> | ||
- | asc() #@ Print the ASCII value of the first character of each argument | ||
- | { #@ USAGE: asc STRING ... | ||
- | for char | ||
- | do | ||
- | printf "%d${EOV-\n}" "'$char" | ||
- | done | ||
- | } | ||
- | </code> | ||
- | [[ http://www.linkedin.com/groups/Fridays-function-asc-Print-ASCII-3716796.S.252009597?view=&gid=3716796&type=member&item=252009597&trk=eml-anet_dig-b_nd-pst_ttle-cn | From Article ]] | ||
- | |||
- | ==== Print argument in reverse ==== | ||
- | <code bash> | ||
- | revword() #@ print each argument with bytes reversed | ||
- | for w | ||
- | do | ||
- | len=${#w} | ||
- | w2= | ||
- | n=0 | ||
- | while (( ${#w2} < len )) | ||
- | do | ||
- | w2=${w:n++:1}$w2 | ||
- | done | ||
- | printf "%s\n" "$w2" | ||
- | done | ||
- | </code> | ||
- | [[ http://www.linkedin.com/groups/Fridays-function-revword-3716796.S.253755071?view=&gid=3716796&type=member&item=253755071&trk=eml-anet_dig-b_nd-pst_ttle-cn | From Article ]] | ||
- | |||
- | ==== 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 "$var" in | ||
- | i) prefix=1 ;; | ||
- | q) quiet=1 ;; | ||
- | esac | ||
- | done | ||
- | shift $(( $OPTIND - 1 )) | ||
- | |||
- | for p | ||
- | do | ||
- | p=${p%"${p##*[!/]}"} ## remove trailing slashes | ||
- | case $p in | ||
- | ""|.) continue ;; | ||
- | esac | ||
- | case :$PATH: in | ||
- | *:$p:*) | ||
- | (( quiet == 0 )) && echo "addpath: $p already in path" >&2 | ||
- | continue | ||
- | ;; | ||
- | esac | ||
- | if [ -d "$p" ] | ||
- | then | ||
- | (( prefix == 1 )) && PATH=$p:$PATH || PATH=$PATH:$p | ||
- | else | ||
- | [ $quiet -eq 0 ] && echo "addpath: $p is not a directory" >&2 | ||
- | fi | ||
- | done | ||
- | export PATH ## probably unnecessary | ||
- | } | ||
- | </code> | ||
- | |||
- | [[ http://www.linkedin.com/groups/Fridays-function-addpath-Add-one-3716796.S.255801510 | From Article ]] | ||
- | |||
- | ==== 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:-1752} last=${2:-2100} var=$3 | ||
- | declare -i year month day dim | ||
- | year=$(( RANDOM % (last-first) + first )) | ||
- | month=$(( RANDOM % 12 + 1 )) | ||
- | days_in_month dim "$month" "$year" | ||
- | day=$(( RANDOM % dim + 1 )) | ||
- | printf -v random_date "%d-%02d-%02d" "$year" "$month" "$day" | ||
- | is_var "$var" && | ||
- | printf -v "$var" %s "$random_date" || | ||
- | printf "%s\n" "$random_date" | ||
- | } | ||
- | </code> | ||
- | |||
- | [[ http://www.linkedin.com/groups/Fridays-function-randomdate-Return-random-3716796.S.5799573907065286656 | From Article ]] | ||
- | |||
- | ==== 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 )) | ||
- | } | ||
- | </code> | ||
- | [[ http://www.linkedin.com/groups/Fridays-function-isfactor-Return-successfully-3716796.S.5794561860833390593 | From Article ]] |