Bash-Funk “strings” module
The following commands are available when this module is loaded:
- -ascii2hex
- -hex2ascii
- -normalize-path
- -str-join
- -str-lower
- -str-matches-glob
- -str-matches-regex
- -str-repeat
- -str-trim
- -str-upper
- -strip-ansi
- -substr-after
- -substr-after-last
- -substr-before
- -substr-before-last
- -substr-between
- -test-all-strings
License
SPDX-FileCopyrightText: © Vegard IT GmbH (https://vegardit.com)
SPDX-License-Identifier: Apache-2.0
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-ascii2hex
Usage: -ascii2hex [OPTION]... ASCII_STRING
Prints the hexa-decimal representation of the given ASCII string.
Requirements:
+ Command 'xxd' must be available.
Parameters:
ASCII_STRING (required)
The ASCII string to convert.
Options:
--help
Prints this help.
--tracecmd
Enables bash debug mode (set -x).
--selftest
Performs a self-test.
--
Terminates the option list.
Examples:
$ -ascii2hex XYZ
58595A
Implementation:
printf "$_ASCII_STRING" | xxd -p | tr "[a-z]" "[A-z]"
-hex2ascii
Usage: -hex2ascii [OPTION]... HEX_STRING
Prints the ASCII representation of the given hexa-decimal string.
Parameters:
HEX_STRING (required)
The hexa-decimal string to convert.
Options:
--help
Prints this help.
--tracecmd
Enables bash debug mode (set -x).
--selftest
Performs a self-test.
--
Terminates the option list.
Examples:
$ -hex2ascii 58595A
XYZ
Implementation:
printf "$_HEX_STRING" | xxd -r -p
-normalize-path
Usage: -normalize-path [OPTION]... PATH
Prints the normalized form of the given file path.
Parameters:
PATH (required)
The path to normalize.
Options:
--help
Prints this help.
--tracecmd
Enables bash debug mode (set -x).
--selftest
Performs a self-test.
--
Terminates the option list.
Examples:
$ -normalize-path a/./b/c/d/../e
a/b/c/e
Implementation:
# Remove all occurrences of "/./"
local normalized=${_PATH//\/.\//\/}
# Remove all occurrences of "dir/.."
while [[ ${normalized} =~ [^\/][^\/]*\/\.\.\/ ]]; do
normalized=${normalized/${BASH_REMATCH[0]}/}
done
echo $normalized
-str-join
Usage: -str-join [OPTION]... SEPARATOR [STRING]...
Prints strings joined with the given separator.
Parameters:
SEPARATOR (required)
The separator to join the strings.
STRING
The strings to join.
Options:
--help
Prints this help.
--tracecmd
Enables bash debug mode (set -x).
--selftest
Performs a self-test.
--
Terminates the option list.
Examples:
$ -str-join , a b c
a,b,c
$ -str-join // a b c
a//b//c
$ -str-join : a b 'c d'
a:b:c d
$ -str-join , a
a
$ -str-join ,
Implementation:
if [[ ${#_STRING[@]} -lt 1 ]]; then
return 0;
fi
local firstItem=${_STRING[0]}
if [[ ${#_STRING[@]} -lt 2 ]]; then
echo $firstItem
return 0;
fi
local additionalItems=("${_STRING[@]:1}")
printf "%s" "$firstItem${additionalItems[@]/#/$_SEPARATOR}"
-str-lower
Usage: -str-lower [OPTION]... STRING
Prints the given string in lower cases.
Parameters:
STRING (required)
The string to convert.
Options:
--help
Prints this help.
--tracecmd
Enables bash debug mode (set -x).
--selftest
Performs a self-test.
--
Terminates the option list.
Examples:
$ -str-lower aBcDeF
abcdef
Implementation:
if ((${BASH_VERSION::1} < 4)); then
echo "$_STRING" | tr '[:upper:]' '[:lower:]'
else
echo "${_STRING,,}"
fi
-str-matches-glob
Usage: -str-matches-glob [OPTION]... GLOB_PATTERN [STRING]...
Matches the given string(s) against the glob pattern, prints the found matches and returns true if at least one match was found.
Parameters:
GLOB_PATTERN (required)
The regex pattern to match the string(s) against.
STRING
The strings to check.
Options:
-a, --all
Specifies that all input strings must match.
-v, --verbose
Prints additional information during command execution.
-----------------------------
--help
Prints this help.
--tracecmd
Enables bash debug mode (set -x).
--selftest
Performs a self-test.
--
Terminates the option list.
Examples:
$ -str-matches-glob c?t
$ -str-matches-glob c?t cat hat
cat
$ -str-matches-glob -v c?t cat hat
match: cat
no match: hat
$ -str-matches-glob -a c?t cat hat
cat
$ -str-matches-glob -v -a c?t cat hat
match: cat
no match: hat
Implementation:
if [[ ! ${_STRING} ]]; then
return 0
fi
local matchFound mismatchFound str
for str in ${_STRING[@]}; do
case "$str" in
$_GLOB_PATTERN)
matchFound=1
if [[ $_verbose ]]; then
echo "match: $str"
else
echo "$str"
fi
;;
*)
mismatchFound=1
[[ $_verbose ]] && echo "no match: $str" || true
;;
esac
done
if [[ $_all ]]; then
[[ $mismatchFound ]] && return 1 || return 0
else
[[ $matchFound ]] && return 0 || return 1
fi
-str-matches-regex
Usage: -str-matches-regex [OPTION]... REGEX_PATTERN [STRING]...
Matches the given string(s) against the regex pattern, prints the found matches and returns true if at least one match was found.
Parameters:
REGEX_PATTERN (required)
The regex pattern to match the string(s) against.
STRING
The strings to check.
Options:
-a, --all
Specifies that all input strings must match.
-v, --verbose
Prints additional information during command execution.
-----------------------------
--help
Prints this help.
--tracecmd
Enables bash debug mode (set -x).
--selftest
Performs a self-test.
--
Terminates the option list.
Examples:
$ -str-matches-regex c.t
$ -str-matches-regex c.t cat hat
cat
$ -str-matches-regex -v c.t cat hat
match: cat
no match: hat
$ -str-matches-regex -a c.t cat hat
cat
$ -str-matches-regex -v -a c.t cat hat
match: cat
no match: hat
Implementation:
if [[ ! ${_STRING} ]]; then
return 0
fi
local matchFound mismatchFound str
for str in ${_STRING[@]}; do
if [[ $str =~ $_REGEX_PATTERN ]]; then
matchFound=1
if [[ $_verbose ]]; then
echo "match: $str"
local i=1 n=${#BASH_REMATCH[*]}
while [[ $i -lt $n ]]; do
echo " group($i): ${BASH_REMATCH[$i]}"
(( i++ ))
done
else
echo "$str"
fi
else
mismatchFound=1
[[ $_verbose ]] && echo "no match: $str" || true
fi
done
if [[ $_all ]]; then
[[ $mismatchFound ]] && return 1 || return 0
else
[[ $matchFound ]] && return 0 || return 1
fi
-str-repeat
Usage: -str-repeat [OPTION]... STRING COUNT
Prints the given string multiple times.
Parameters:
STRING (required)
The string to repeat.
COUNT (required, integer: 1-?)
Number of times to repeat the string.
Options:
--help
Prints this help.
--tracecmd
Enables bash debug mode (set -x).
--selftest
Performs a self-test.
--
Terminates the option list.
Examples:
$ -str-repeat a 3
aaa
Implementation:
local spaces="$(printf "%${_COUNT}s" "")"
echo "${spaces// /$_STRING}"
-str-trim
Usage: -str-trim [OPTION]... STRING
Prints the given string without leading and trailing spaces.
Parameters:
STRING (required)
The string to trim.
Options:
--help
Prints this help.
--tracecmd
Enables bash debug mode (set -x).
--selftest
Performs a self-test.
--
Terminates the option list.
Examples:
$ -str-trim " abc "
abc
Implementation:
echo $_STRING
-str-upper
Usage: -str-upper [OPTION]... STRING
Prints the given string in upper cases.
Parameters:
STRING (required)
The string to convert.
Options:
--help
Prints this help.
--tracecmd
Enables bash debug mode (set -x).
--selftest
Performs a self-test.
--
Terminates the option list.
Examples:
$ -str-upper aBcDeF
ABCDEF
Implementation:
if ((${BASH_VERSION::1} < 4)); then
echo "$_STRING" | tr '[:lower:]' '[:upper:]'
else
echo "${_STRING^^}"
fi
-strip-ansi
Usage: -strip-ansi [OPTION]... [STRING]...
Removes any ANSI escape sequences from the given string or from stdin.
Parameters:
STRING
The strings to strip.
Options:
--help
Prints this help.
--tracecmd
Enables bash debug mode (set -x).
--selftest
Performs a self-test.
--
Terminates the option list.
Examples:
$ -strip-ansi $(echo -e '\033[4mThis is underlined\033[24m')
This is underlined
Implementation:
local sedCommand="sed"
if [[ $OSTYPE == "darwin"* ]]; then
if hash gsed &>/dev/null; then
sedCommand="gsed --unbuffered"
fi
else
sedCommand+=" --unbuffered"
fi
sedCommand+=" $'s,\x1b\\[[0-9;]*[a-zA-Z],,g'"
if [[ ${_STRING} ]]; then
echo "${_STRING[@]}" | eval $sedCommand
else
eval $sedCommand
fi
-substr-after
Usage: -substr-after [OPTION]... SEARCH_IN SEARCH_FOR
Prints the substring after the first occurrence of SEARCH_FOR.
Parameters:
SEARCH_IN (required)
The string to search.
SEARCH_FOR (required)
The separator.
Options:
--help
Prints this help.
--tracecmd
Enables bash debug mode (set -x).
--selftest
Performs a self-test.
--
Terminates the option list.
Examples:
$ -substr-after 00aa11aa22 aa
11aa22
Implementation:
echo ${_SEARCH_IN#*$_SEARCH_FOR}
-substr-after-last
Usage: -substr-after-last [OPTION]... SEARCH_IN SEARCH_FOR
Prints the substring after the last occurrence of SEARCH_FOR.
Parameters:
SEARCH_IN (required)
The string to search.
SEARCH_FOR (required)
The separator.
Options:
--help
Prints this help.
--tracecmd
Enables bash debug mode (set -x).
--selftest
Performs a self-test.
--
Terminates the option list.
Examples:
$ -substr-after-last 00aa11aa22 aa
22
Implementation:
echo "${_SEARCH_IN#${_SEARCH_IN%${_SEARCH_FOR}*}$_SEARCH_FOR}"
-substr-before
Usage: -substr-before [OPTION]... SEARCH_IN SEARCH_FOR
Prints the substring before the first occurrence of SEARCH_FOR.
Parameters:
SEARCH_IN (required)
The string to search.
SEARCH_FOR (required)
The separator.
Options:
--help
Prints this help.
--tracecmd
Enables bash debug mode (set -x).
--selftest
Performs a self-test.
--
Terminates the option list.
Examples:
$ -substr-before 00aa11aa22 aa
00
Implementation:
echo "${_SEARCH_IN%%${_SEARCH_FOR}*}"
-substr-before-last
Usage: -substr-before-last [OPTION]... SEARCH_IN SEARCH_FOR
Prints the substring before the last occurrence of SEARCH_FOR.
Parameters:
SEARCH_IN (required)
The string to search.
SEARCH_FOR (required)
The separator.
Options:
--help
Prints this help.
--tracecmd
Enables bash debug mode (set -x).
--selftest
Performs a self-test.
--
Terminates the option list.
Examples:
$ -substr-before-last 00aa11aa00 aa
00aa11
Implementation:
echo "${_SEARCH_IN%${_SEARCH_FOR}*}"
-substr-between
Usage: -substr-between [OPTION]... SEARCH_IN PREFIX SUFFIX
Prints the substring between PREFIX and SUFFIX.
Parameters:
SEARCH_IN (required)
The string to search.
PREFIX (required)
The start separator.
SUFFIX (required)
The end separator.
Options:
--help
Prints this help.
--tracecmd
Enables bash debug mode (set -x).
--selftest
Performs a self-test.
--
Terminates the option list.
Examples:
$ -substr-between 00aa11aa22aa00 aa aa
11
Implementation:
local withoutPrefix="${_SEARCH_IN#*$_PREFIX}"
echo "${withoutPrefix%%${_SUFFIX}*}"
-test-all-strings
Usage: -test-all-strings [OPTION]...
Performs a selftest of all functions of this module by executing each function with option '--selftest'.
Options:
--help
Prints this help.
--tracecmd
Enables bash debug mode (set -x).
--selftest
Performs a self-test.
--
Terminates the option list.
Implementation:
-ascii2hex --selftest && echo || return 1
-hex2ascii --selftest && echo || return 1
-normalize-path --selftest && echo || return 1
-str-join --selftest && echo || return 1
-str-lower --selftest && echo || return 1
-str-matches-glob --selftest && echo || return 1
-str-matches-regex --selftest && echo || return 1
-str-repeat --selftest && echo || return 1
-str-trim --selftest && echo || return 1
-str-upper --selftest && echo || return 1
-strip-ansi --selftest && echo || return 1
-substr-after --selftest && echo || return 1
-substr-after-last --selftest && echo || return 1
-substr-before --selftest && echo || return 1
-substr-before-last --selftest && echo || return 1
-substr-between --selftest && echo || return 1