Data types functions.
Contains functions to manipulate different data types such as strings, arrays, associative arrays (hashes), lists, sets, regexps, datetime and settings.
It contains the following classes:
* string
* array
* hash
* list
* set
* regexp
* datetime
Use the command module.doc <function_name>
to see the documentation for a function (see an example)
- string_append()
- string_split()
- string_split_()
- string_contains-word()
- array_find-indexes_()
- array_find_()
- array_find()
- array_include()
- array_intersection_()
- array_remove-at()
- array_remove()
- array_remove-values()
- array_defined()
- array_init()
- array_uniq_()
- array_eq()
- array_to_s()
- array_join()
- list_find_()
- list_include()
- hash_defined()
- hash_init()
- hash_has-key()
- hash_merge()
- hash_zip()
- hash_copy()
- hash_find-value_()
- hash_eq()
- set_eq()
- regexp_escape-bash-pattern_()
- regexp_escape-ext-regexp-pattern_()
- regexp_escape-regexp-replace_()
- datetime_interval-to-sec_()
Append a string to the content of the provided variable, optionally prefixing it with a separator if the variable is not empty.
- string.append
- string.concat
- $1 (String): Variable name
- $2 (String): String to append
- $3 (String)[default: " "]: Separator
- -m|--multi-line: Append the string to every line of the destination variable
- Concatenation of the two strings, optionally separated by the provided separator
Split a string based on a separator and put the resulting components into the provided array.
- string.split
- $1 (String): The string to split
- $2 (String): The array name
- $3 (String)[default: space]: Separator (can be an empty string, in which case the string will be split into its component characters)
Split a string based on a separator and return the array containing its elements.
- string.split_
- $1 (String): The string to split
- $2 (String)[default: space]: Separator (can be an empty string, in which case the string will be split into its component characters)
- An array containing the elements composing the string
Check if a string contains a word (needs to be separated by space or tab from other possible words).
- string.contains-word_
- $1 (String): The string to check
- $2 (String): The word to find
- @exitcodes 0 if found, 1 otherwise
Return the list of array's indexes which have the provided value.
- array.find-indexes_
- $1 (String): Array name
- $2 (String): Value to find
- An array of indexes of the array containing the provided value.
- 0 if at least one item in array is found, 1 otherwise
$ declare -a ary=(a b c "s 1" d e "s 1")
$ array.find-indexes_ ary "s 1"
# return __a=(3 6)
Return the index of the array containing the provided value, or -1 if not found.
- array.find_
- $1 (String): Array name
- $2 (String): Value to find
- The index of the array containing the provided value, or -1 if not found.
- 0 if found, 1 if not found
$ declare -a ary=(a b c "s 1" d e "s 1")
$ array.find_ ary "s 1"
# return __=3
Print the index of the array containing the provided value, or -1 if not found.
It have the same syntax as array.find_
but print the index found on stdout instead of the global variable $__
- array.find_()
Check whether an item is present in the provided array.
- array.include
- $1 (String): Array name
- $2 (String): Value to find
- 0 if found, 1 if not found
$ declare -a ary=(a b c "s 1" d e "s 1")
$ array.include ary "s 1"
# exitcode=0
Return an array containing the intersection between two arrays.
- array.intersection_
- $1 (String): First array name
- $2 (String): Second array name
- An array containing the intersection of the two provided arrays.
- 0 if the intersection contains at least one element, 1 otherwise
$ declare -a ary1=(a b c d e f)
$ declare -a ary2=(b d g h)
$ array.intersection_ ary1 ary2
# return __a=(b d)
Remove the item at the provided index from array.
- array.remove-at
- $1 (String): Array name
- $2 (String): Index of the item to remove
$ declare -a ary=(a b c d e f)
$ array.remove-at ary 2
$ declare -p ary
declare -a ary=([0]="a" [1]="b" [2]="d" [3]="e" [4]="f")
Remove the first instance of the provided item from array.
- array.remove
- $1 (String): Array name
- $2 (String): Item to remove
- 0 if item is found and removed, 1 otherwise
$ declare -a ary=(a b c d e a)
$ array.remove ary a
$ declare -p ary
declare -a ary=([0]="b" [1]="c" [2]="d" [3]="e" [4]="a")
Remove any occurrence of the provided item from array.
- array.remove-values
- $1 (String): Array name
- $2 (String): Item to remove
$ declare -a ary=(a b c d e a)
$ array.remove-values ary a
$ declare -p ary
declare -a ary=([0]="b" [1]="c" [2]="d" [3]="e")
Check whether an array with the provided name exists.
- array.defined
- $1 (String): Array name
- Standard (0 for true, 1 for false)
Initialize an array (resetting it if already existing).
- array.init
- $1 (String): Array name
Return an array with duplicates removed from the provided array.
- array.uniq_
- $1 (String): Array name
- Array with duplicates removed
$ declare -a ary=(1 2 1 5 6 1 7 2)
$ array.uniq_ "${ary[@]}"
$ declare -p __a
declare -a __a=([0]="1" [1]="2" [2]="5" [3]="6" [4]="7")
Compare two arrays
- array.eq
- $1 (String): First array name
- $2 (String): Second array name
- 0 if the array are equal, 1 otherwise
$ declare -a ary1=(1 2 3)
$ declare -a ary2=(1 2 3)
$ array.eq ary1 ary2
# exitcode=0
Print a string with the definition of the provided array or hash (as shown in declare -p
but without the first part declaring the variable).
- array.to_s
- hash.to_s
- $1 (String): Array name
$ declare -a ary=(1 2 3)
$ array.to_s ary
([0]="1" [1]="2" [2]="3")
Join the elements of the provided array into a string, using a provided string as a separator.
- array.join
- $1 (String): Array name, whose elements will be joined into the string
- $2 (String): Variable name for the output
- $3 (String)[default: space]: A separator
Return the index inside a list in which appear the provided searched item.
- list.find_
- list_include
- list.include
- $1 (String): Item to find
- ... (String): Elements of the list
- The index inside the list in which appear the provided item.
- 0 if the item is found, 1 otherwise
Check whether an item is included in a list of values.
- list.include
- $1 (String): Item to find
- ... (String): Elements of the list
- 0 if the item is found, 1 otherwise
Check whether an hash with the provided name exists.
- hash.defined
- $1 (String): Hash name
- Standard (0 for true, 1 for false)
Initialize an hash (resetting it if already existing).
- hash.init
- $1 (String): Hash name
Check whether a hash contains the provided key.
- hash.has-key
- $1 (String): Hash name
- $2 (String): Key name to find
- Standard (0 for true, 1 for false)
Merge two hashes.
- hash.merge
- $1 (String): Variable name of the 1st hash, in which to merge the 2nd hash
- $2 (String): Variable name of the 2nd hash, which is merged into the 1st hash
$ declare -A h1=([a]=1 [b]=2 [e]=3)
$ declare -A h2=([a]=5 [c]=6)
$ hash.merge h1 h2
$ declare -p h1
declare -A h1=([a]="5" [b]="2" [c]="6" [e]="3" )
Create an hash from two arrays, one with the keys and the second with the respective values
- hash.zip
- $1 (String): Variable name of the resulting hash
- $2 (String): Variable name of the array containing the list of keys
- ... (String): List of values
- 1 if number of keys is greater than number of values; 2 if number of values is greater of number of keys; 0 otherwise
$ declare -a keys=([key1 key2 key3)
$ declare -a values=(val1 "val2 x" val3)
$ hash.zip hash keys "${values[@]}"
$ declare -p hash
declare -A hash=([kay1]="val1" [key2]="key2 x" [key3]="val3")
Copy an hash.
- hash.copy
- $1 (String): Variable name of the hash to copy from
- $2 (String): Variable name of the hash to copy to: if the hash is not yet defined, it will be created as a global hash
$ declare -A h1=([a]=1 [b]=2 [e]=3)
$ hash.copy h1 h2
$ declare -p h2
declare -A h2=([a]="1" [b]="2" [e]="3")
Return the key of the hash which have the provided value.
- hash.find-value_
- $1 (String): Hash name
- $2 (String): Value to find
$ declare -A h1=([a]=1 [b]=2 [e]=3)
$ hash.find-value_ h1 2
$ echo $__
b
Compare two hashes
- hash.eq
- $1 (String): First hash name
- $2 (String): Second hash name
- 0 if the hashes are equal, 1 otherwise
$ declare -a h1=([key1]=val1 [key2]=val2)
$ declare -a h2=([key1]=val1 [key2]=val2)
$ hash.eq h1 h2
# exitcode=0
Compare two sets (a set is an array where index associated to values are negligibles)
- set.eq
- $1 (String): First array name
- $2 (String): Second array name
- 0 if the values of arrays are the same, 1 otherwise
$ declare -a ary1=(1 2 3 1 1)
$ declare -a ary2=(3 2 1 2 2)
$ set.eq ary1 ary2
# exitcode=0
Escape a string which have to be used as a search pattern in a bash parameter expansion as ${parameter/pattern/string}.
The escaped characters are %*[?/
- regexp.escape-bash-pattern_
- $1 (String): String to be escaped
- Escaped string
$ regexp.escape-bash-pattern_ 'a * x #'
# return __=a \* x \#
Escape a string which have to be used as a search pattern in a extended regexp in sed
or grep
.
The escaped characters are the following: {$.*[\^|]
.
- regexp.escape-ext-regexp-pattern_
- $1 (String): String to be escaped
- $2 (String)[default: /]: Separator used in the
sed
expression
- Escaped string
$ regexp.escape-ext-regexp-pattern_ "[WW]" "W"
# return __=\[\W\W[]]
Escape a string which have to be used as a replace string on a sed
command.
The escaped characters are the separator character and the following characters: /&
.
- regexp.escape-ext-regexp-pattern_
- $1 (String): String to be escaped
- $2 (String)[default: /]: Separator used in the
sed
expression
- Escaped string
$ regexp.escape-regexp-replace_ "p/x"
# return __="p\/x"
$ regexp.escape-regexp-replace_ "x//" "x"
# return __="\x//"
Convert the provided time interval to a seconds interval. The format of the time interval is the following:
[<n>d] [<n>h] [<n>m] [<n>s]
- datetime.interval-to-sec_
- ... (String): Any of the following time intervals: <n>d (<n> days), <n>h (<n> hours), <n>m (<n> minutes) and <n>s (<n> seconds)
$ datetime.interval-to-sec_ 1d 2h 3m 45s
# return __=93825