X-Git-Url: https://zdv2.bktei.com/gitweb/BK-2020-03.git/blobdiff_plain/6902250a188de0d54be60ac9f2d7075c37176b01..c06f074c25d1ff3e692cd75fc0219ca3cf8020ba:/unitproc/bkt-insertStr?ds=sidebyside diff --git a/unitproc/bkt-insertStr b/unitproc/bkt-insertStr index 6cea251..5fdbf93 100755 --- a/unitproc/bkt-insertStr +++ b/unitproc/bkt-insertStr @@ -1,21 +1,23 @@ #!/usr/bin/env bash # Desc: Insert string into provided string -yell() { echo "$0:${FUNCNAME[*]}: $*" >&2; } +yell() { echo "$0: $*" >&2; } die() { yell "$*"; exit 111; } must() { "$@" || die "cannot $*"; } - insertStr() { - # Desc: Inserts a + # Desc: Inserts a string into another string at a specified position. # Input: arg1: str str_rec String to receive insertion # arg2: int pos Insertion position (0 = append to front) # arg3: str str_ins String to be inserted # Output: stdout: Combined string - # Version: 0.0.1 + # Version: 0.0.2 # Depends: BK-2020-03: yell(), die(), must() # Ref/Attrib: * BK-2020-03: https://gitlab.com/baltakatei/baltakatei-exdev/ - - local str_rec pos str_ins; + # * Cooper, Mendel. “Advanced Bash-Scripting Guide: Manipulating Strings”. tldp.org https://tldp.org/LDP/abs/html/string-manipulation.html + + local str_rec pos str_ins re len_str_rec; + local pfx_pos_start pfx_len pfx; + local sfx_pos_start sfx_len sfx; # Check args if [[ $# -ne 3 ]]; then @@ -31,7 +33,6 @@ insertStr() { # Calculate string stats len_str_rec="${#str_rec}"; - len_str_ins="${#str_ins}"; # Form prefix pfx_pos_start="0"; @@ -44,7 +45,7 @@ insertStr() { sfx="${str_rec:$sfx_pos_start:$sfx_len}"; # Print output to stdout - printf "%s%s%s\n" "$pfx" "$str_ins" "$sfx"; + printf "%s%s%s\n" "$pfx" "$str_ins" "$sfx"; }; # Insert string provided at indicated position via stdout