This shows you the differences between two versions of the page.
bash_functions [2013/05/18 17:56] k2patel [die # Exit with error code and optional message] |
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 ]] |