Skip to content
This repository has been archived by the owner on Jan 28, 2021. It is now read-only.

Latest commit

 

History

History
1229 lines (909 loc) · 25.6 KB

references.md

File metadata and controls

1229 lines (909 loc) · 25.6 KB
NAME

string_trim -- remove the white chars from prefix and suffix

SYNOPSIS
string_trim [string]
DESCRIPTION
  • [string] the string to process, if absent, it will be read from the standard input (CTRL+D to end)
EXAMPLES
string_trim " as fd "
string_trim < logfile
echo " add " | string_trim
SEE_ALSO

NAME

string_repeat -- make a string by repeat n times of a token string

SYNOPSIS
string_repeat string [nbTimes]
DESCRIPTION
  • string the string to be repeated
  • [nbTimes] the number of times, if absent, it will be read from the standard input (CTRL+D to end)
EXAMPLES
string_repeat 'abc' 5
echo 5 | string_repeat 'abc'
SEE_ALSO

NAME

string_length -- return the string length

SYNOPSIS
string_length [string]
DESCRIPTION
  • [string] the string to process, if absent, it will be read from the standard input (CTRL+D to end)
EXAMPLES
string_length " as fd "
string_length < logfile
echo " add " | string_length
SEE_ALSO

NAME

string_is_empty -- exit success code 0 if the string is empty

SYNOPSIS
string_is_empty [string]
DESCRIPTION
  • [string] the string to process, if absent, it will be read from the standard input (CTRL+D to end)
EXAMPLES
string_is_empty " as fd "
string_is_empty < logfile
echo " add " | string_is_empty
SEE_ALSO

string_length


NAME

string_revert -- revert the characters of a string

SYNOPSIS
string_revert [string]
DESCRIPTION
  • [string] the string to be reverted, if absent, it will be read from the standard input (CTRL+D to end)
EXAMPLES
string_revert 'aBc'
echo 'aBc' | string_revert
SEE_ALSO

NAME

string_upper -- convert all characters to upper case

SYNOPSIS
string_upper [string]
DESCRIPTION
  • [string] the string to be converted, if absent, it will be read from the standard input (CTRL+D to end)
EXAMPLES
string_upper 'abc'
echo 'abc' | string_upper
SEE_ALSO

string_upper_first, string_lower


NAME

string_lower -- convert all characters to lower case

SYNOPSIS
string_lower [string]
DESCRIPTION
  • [string] the string to be converted, if absent, it will be read from the standard input (CTRL+D to end)
EXAMPLES
string_lower 'aBc'
echo 'aBc' | string_lower
SEE_ALSO

string_upper, string_upper_first


NAME

string_upper_first -- convert the first characters to upper case, and the others to lower case

SYNOPSIS
string_upper_first [string]
DESCRIPTION
  • [string] the string to be converted, if absent, it will be read from the standard input (CTRL+D to end)
EXAMPLES
string_upper_first 'aBc'
echo 'aBc' | string_upper_first
SEE_ALSO

string_lower, string_upper


NAME

string_sub -- extract a part of string and return

SYNOPSIS
string_sub startIndex subStringLength [string]
DESCRIPTION
  • startIndex the index of first character in string, 0 based, may negative
  • subStringLength the length of sub string, 0 based, may negative
  • [string] the string to process, if absent, it will be read from the standard input (CTRL+D to end)
EXAMPLES
string_sub -5 -1 " as fd "
string_sub 3 5 < temp_file.txt
echo ' as fd ' | string_sub 2 4
SEE_ALSO

NAME

string_match -- test if the string match the regular expression

SYNOPSIS
string_match regExp [string]
DESCRIPTION
  • regExp the regular expression
  • [string] the string to process, if absent, it will be read from the standard input (CTRL+D to end)
EXAMPLES
string_match 'name;+' "name;name;"
SEE_ALSO

string_index_first


NAME

escape_sed -- escape preserved char of regex, normally for preprocessing of sed token.

SYNOPSIS
escape_sed string
DESCRIPTION
  • string the string to process
EXAMPLES
escape_sed 'a$'
SEE_ALSO

string_replace


NAME

string_replace -- replace literally the token string to new string, not support regular expression

SYNOPSIS
string_replace tokenString newString [string]
DESCRIPTION
  • tokenString the string to search, the preserved character of regular expression will be escaped
  • newString the new value of replacing to, the preserved character of regular expression will be escaped
  • [string] the string to process, if absent, it will be read from the standard input (CTRL+D to end)
EXAMPLES
string_replace 'a' 'b' 'aaa'   ==> 'bbb'
string_replace '$' 'b' 'a$a'   ==> 'aba'
string_replace '\*' 'b' 'a*a'  ==> 'aba'
SEE_ALSO

escape_sed, string_replace_regex


NAME

string_replace_regex -- replace the token string to new string, support regular expression

SYNOPSIS
string_replace_regex tokenString newString [string]
DESCRIPTION
  • tokenString the string to search, support regular expression and its modern extension
  • newString the new value of replacing to, support back-references
  • [string] the string to process, if absent, it will be read from the standard input (CTRL+D to end)
EXAMPLES
string_replace_regex 'a*' 'b' 'a*a' ==> 'b*b'
string_replace_regex 'a*' 'b' "aaa" ==> 'b'
string_replace_regex '*' 'b' 'a*a'  ==> 'aba'
SEE_ALSO

string_replace


NAME

string_index_first -- return the positive index of first place of token in string, -1 if not existed

SYNOPSIS
string_index_first tokenString [string]
DESCRIPTION
  • tokenString the string to search
  • [string] the string to process, if absent, it will be read from the standard input (CTRL+D to end)
EXAMPLES
string_index_first "s f" " as fd "
string_index_first "token" < logfile
echo " add " | string_index_first "token"
SEE_ALSO

string_before_first, string_after_first


NAME

string_before_first -- find the first index of token in string, and return the sub string before it.

SYNOPSIS
string_before_first tokenString [string]
DESCRIPTION
  • tokenString the string to search
  • [string] the string to process, if absent, it will be read from the standard input (CTRL+D to end)
EXAMPLES
string_before_first "s f" " as fd "
string_before_first "str" < logfile
echo " add " | string_before_first "dd"
SEE_ALSO

string_index_first, string_after_first


NAME

string_after_first -- find the first index of token in string, and return the sub string after it.

SYNOPSIS
string_after_first tokenString [string]
DESCRIPTION
  • tokenString the string to search
  • [string] the string to process, if absent, it will be read from the standard input (CTRL+D to end)
EXAMPLES
string_after_first "s f" " as fd "
string_after_first "str" < logfile
echo " add " | string_after_first "dd"
SEE_ALSO

string_index_first, string_before_first


NAME

string_split_to_array -- split a string to array by a delimiter character, then assign the array to a new variable name

SYNOPSIS
string_split_to_array tokenString [newArrayVarName] [string]
DESCRIPTION
  • tokenString the delimiter string
  • [newArrayVarName] optional, the variable name of result array, if absent, the mapped array will be joined by newline and printed to stdout
  • [string] the string to process, if absent, it will be read from the standard input (CTRL+D to end)
EXAMPLES
str="a|b|c"
string_split_to_array '|' newArray "$str"

branchesToSelectString=$(git branch -r --list  'origin/*')
string_split_to_array $'
' branchesToSelectArray "${branchesToSelectString}"
SEE_ALSO

array_join, array_describe, array_from_describe, string_pick_to_array


NAME

string_pick_to_array -- take value using start token and end token from a string to array, then assign the array to a new variable name

SYNOPSIS
string_pick_to_array startTokenString endTokenString [newArrayVarName] [string]
DESCRIPTION
  • startTokenString the start token string
  • endTokenString the end token string
  • [newArrayVarName] optional, the variable name of result array, if absent, the mapped array will be joined by newline and printed to stdout
  • [string] the string to process, if absent, it will be read from the standard input (CTRL+D to end)
EXAMPLES
str="[{age:12},{age:15},{age:16}]"
string_pick_to_array '{age:' '}' newArray "$str"
SEE_ALSO

array_join, array_describe, array_from_describe, string_split_to_array


NAME

array_join -- join an array to string using delimiter string

SYNOPSIS
array_join delimiter arrayVarName
DESCRIPTION
  • delimiter the delimiter string
  • arrayVarName the variable name of the array to be processed
EXAMPLES
myArry=(" a " " b c ")
array_join '|' myArry ==> " a | b c "
SEE_ALSO

string_split_to_array, array_describe, array_from_describe


NAME

array_describe -- convert the array to its string representation

SYNOPSIS
array_describe arrayVarName
DESCRIPTION
  • arrayVarName the variable name of the array to be processed
EXAMPLES
myArray=("a" "b")
array_describe myArray ==> ([0]='a' [1]='b')
SEE_ALSO

string_split_to_array, array_join, array_from_describe


NAME

array_from_describe -- restore the array from its string representation, then assign it to a variable name

SYNOPSIS
array_from_describe newArrayVarName [string]
DESCRIPTION
  • newArrayVarName the new variable name which the array will be assigned to
  • [string] the string of array describe, if absent, it will be read from the standard input (CTRL+D to end)
EXAMPLES
array_from_describe myNewArray "([0]='a' [1]='b')"
array_from_describe myNewArray < fileNameContentString
SEE_ALSO

string_split_to_array, array_join, array_describe


NAME

array_contains -- exit success code 0 if array contains element, fail if not.

SYNOPSIS
array_contains arrayVarName [seekingElement]
DESCRIPTION
  • arrayVarName the variable name of array to test
  • [seekingElement] the element to search in array, if absent, it will be read from the standard input (CTRL+D to end)
EXAMPLES
arr=("a" "b" "c" "ab" "f" "g")
array_contains arr "ab"
echo "ab" | array_contains arr
SEE_ALSO

array_remove


NAME

array_sort -- sort the elements of array, save the result to original variable name

SYNOPSIS
array_sort arrayVarName
DESCRIPTION
  • arrayVarName the variable name of the array to be processed
EXAMPLES
myArray=('aa' 'bb' 'aa')
array_sort myArray ==> ([0]='aa' [1]='aa' [2]='bb')
SEE_ALSO

array_sort_distinct


NAME

array_sort_distinct -- remove the duplicated elements of array, sort and save the result to original variable name

SYNOPSIS
array_sort_distinct arrayVarName
DESCRIPTION
  • arrayVarName the variable name of the array to be processed
EXAMPLES
myArray=('aa' 'bb' 'aa')
array_sort_distinct myArray ==> ([0]='aa' [1]='bb')
SEE_ALSO

array_sort


NAME

array_length -- return the number of elements of array

SYNOPSIS
array_length arrayVarName
DESCRIPTION
  • arrayVarName the variable name of the array to be processed
EXAMPLES
myArray=('aa' 'bb' 'aa')
array_length myArray ==> 3
SEE_ALSO

NAME

array_reset_index -- reset the indexes of array to the sequence 0,1,2..., save the result to original variable name

SYNOPSIS
array_reset_index arrayVarName
DESCRIPTION
  • arrayVarName the variable name of the array to be processed
EXAMPLES
myArray=([2]='a' [5]='c' [11]='dd')
array_reset_index myArray ==> ([0]='a' [1]='c' [2]='dd')
SEE_ALSO

NAME

array_equals -- test if the elements of 2 array are equal, ignore the array index

SYNOPSIS
array_equals arrayVarName1 arrayVarName2 [ignoreOrder] [ignoreDuplicated]
DESCRIPTION
  • arrayVarName1 the variable name of an array
  • arrayVarName2 the variable name of another array to compare with
  • [ignoreOrder] optional, a boolean value true/false, indicate whether ignore element order when compare, default true
  • [ignoreDuplicated] optional, a boolean value true/false, indicate whether ignore element duplicated when compare, default false
EXAMPLES
myArray1=('aa' [3]='bb' 'aa')
myArray2=('aa' 'aa' 'bb')
array_equals myArray1 myArray2 false && echo Y || echo N ==> N
array_equals myArray1 myArray2 true && echo Y || echo N ==> Y
SEE_ALSO

NAME

array_intersection -- calcul the intersection of 2 arrays, and save the result to a new variable

SYNOPSIS
array_intersection arrayVarName1 arrayVarName2 newArrayVarName [ignoreOrderAndDuplicated]
DESCRIPTION
  • arrayVarName1 the variable name of an array
  • arrayVarName2 the variable name of another array
  • newArrayVarName the name of new variable to save the result
  • [ignoreOrderAndDuplicated] optional, a boolean value true/false, indicate whether ignore element duplicated and order them when save the result, default true
EXAMPLES
myArray1=('aa' [3]='bb' 'aa' 'cc')
myArray2=('aa' 'aa' 'dd' 'bb')
array_intersection myArray1 myArray2 newArray
array_intersection myArray1 myArray2 newArray false
SEE_ALSO

array_subtract, array_union


NAME

array_subtract -- calcul the subtract of 2 arrays, and save the result to a new variable

SYNOPSIS
array_subtract arrayVarName1 arrayVarName2 newArrayVarName [ignoreOrderAndDuplicated]
DESCRIPTION
  • arrayVarName1 the variable name of an array
  • arrayVarName2 the variable name of another array
  • newArrayVarName the name of new variable to save the result
  • [ignoreOrderAndDuplicated] optional, a boolean value true/false, indicate whether ignore element duplicated and order them when save the result, default true
EXAMPLES
myArray1=('aa' [3]='bb' 'aa' 'cc')
myArray2=('aa' 'aa' 'dd' 'bb')
array_subtract myArray1 myArray2 newArray
array_subtract myArray1 myArray2 newArray false
SEE_ALSO

array_intersection, array_union


NAME

array_union -- calcul the union of 2 arrays, and save the result to a new variable

SYNOPSIS
array_union arrayVarName1 arrayVarName2 newArrayVarName [ignoreOrderAndDuplicated]
DESCRIPTION
  • arrayVarName1 the variable name of an array
  • arrayVarName2 the variable name of another array
  • newArrayVarName the name of new variable to save the result
  • [ignoreOrderAndDuplicated] optional, a boolean value true/false, indicate whether ignore element duplicated and order them when save the result, default true
EXAMPLES
myArray1=('aa' [3]='bb' 'aa' 'cc')
myArray2=('aa' 'aa' 'dd' 'bb')
array_union myArray1 myArray2 newArray
array_union myArray1 myArray2 newArray false
SEE_ALSO

array_intersection, array_union


NAME

array_append -- append some elements to original array

SYNOPSIS
array_append arrayVarName element...
DESCRIPTION
  • arrayVarName the variable name of array to process
  • element... the elements to append to array
EXAMPLES
myArray=()
array_append myArray "ele ment1" "ele ment2"
SEE_ALSO

array_remove


NAME

array_remove -- remove the element from the original array

SYNOPSIS
array_remove arrayVarName element
DESCRIPTION
  • arrayVarName the variable name of array to process
  • element the element to remove from array
EXAMPLES
arr=("a" "b" "c" "ab" "f" "g")
array_remove arr "ab"
SEE_ALSO

array_contains, array_append


NAME

array_clone -- clone an array, including index/order/duplication/value, and assign the result array to a new variable name

SYNOPSIS
array_clone arrayVarName newArrayVarName
DESCRIPTION
  • arrayVarName the variable name of array to process
  • newArrayVarName the variable name of result array
EXAMPLES
arr=(" a " " b c ")
array_clone arr newArray
SEE_ALSO

NAME

array_map -- apply the specified map operation on each element of array, and assign the result array to a new variable name

SYNOPSIS
array_map arrayVarName pipedOperators [newArrayVarName]
DESCRIPTION
  • arrayVarName the variable name of array to process
  • pipedOperators a string of operations, if multiple operations will be apply on each element, join them by pipe '|'
  • [newArrayVarName] optional, the variable name of result array, if absent, the mapped array will be joined by newline and printed to stdout
EXAMPLES
arr=(" a " " b c ")
array_map arr "string_trim | wc -m | string_trim" newArray
SEE_ALSO

NAME

array_filter -- filter the elements of an array, and assign the result array to a new variable name

SYNOPSIS
array_filter arrayVarName regExp [newArrayVarName]
DESCRIPTION
  • arrayVarName the variable name of array to process
  • regExp a string of regular expression pattern
  • [newArrayVarName] optional, the variable name of result array, if absent, the mapped array will be joined by newline and printed to stdout
EXAMPLES
arr=("NAME A" "NAME B" "OTHER")
array_filter arr 'NAME' newArray
SEE_ALSO

NAME

args_parse -- parse the script argument values to positional variable names, process firstly the optional param help(-h) / quiet(-q) if existed

SYNOPSIS
args_parse $# "$##### " positionalVarName...
DESCRIPTION
  • positionalVarName... some new variable names to catch the positional argument values
EXAMPLES
args_parse $# "$##### " newVar1 newVar2 newVar3
SEE_ALSO

NAME

args_valid_or_select -- test whether the value contains by the array, if not contained, require to select a new one from array and assign it to the value variable name

SYNOPSIS
args_valid_or_select valueVarName arrayVarName prompt
DESCRIPTION
  • valueVarName the variable name of the value to valid and the new value assign to,
  • arrayVarName the variable name of array
  • prompt the prompt message to show when requiring to select a new one from array
EXAMPLES
arr=("a" "b" "c" "ab" "f" "g")
appName="abc"
args_valid_or_select appName arr "Which app"
varEmpty=""
args_valid_or_select varEmpty arr "Which app"
SEE_ALSO

args_valid_or_select_pipe, args_valid_or_read


NAME

args_valid_or_select_pipe -- test whether the value contains by the array, if not contained, require to select a new one from array and assign it to the value variable name

SYNOPSIS
args_valid_or_select_pipe valueVarName strValidValues prompt
DESCRIPTION
  • valueVarName the variable name of the value to valid and the new value assign to,
  • strValidValues values joined by pipe '|'
  • prompt the prompt message to show when requiring to select a new one from array
EXAMPLES
sel="abc"
args_valid_or_select_pipe sel "a|ab|d" "which value"
SEE_ALSO

args_valid_or_select, args_valid_or_read


NAME

args_valid_or_read -- test whether the value matched the valid regular expression, if not matched, require input a new one and assign it to the value variable name

SYNOPSIS
args_valid_or_read valueVarName strRegExp prompt [proposedValue]
DESCRIPTION
  • valueVarName the variable name of the value to valid and the new value assign to,
  • strRegExp a string of regular expression to be used for validation
  • prompt the prompt message to show when requiring to read a new one from stdin
  • [proposedValue] the proposed spare value to show for user, or to used when quite mode
EXAMPLES
args_valid_or_read destProjectSIA '^[0-9a-z]{3,3}$' "SIA (lowercase, 3 chars)"
args_valid_or_read destProjectIRN '^[0-9]{5,5}$' "IRN (only the 5 digits)"
args_valid_or_read destRootPackage '^.+$' "Destination root package" "${defaultDestRootPackage}"
SEE_ALSO

args_valid_or_select, args_valid_or_select_pipe


NAME

args_print -- show the name and value of variables

SYNOPSIS
args_print variableName...
DESCRIPTION
  • variableName... some existed variable names to show its value
EXAMPLES
var1="value 1"
var2="value 2"
args_print var1 var2
SEE_ALSO

args_confirm


NAME

args_confirm -- show the name and value of variables, and continue execute if confirmed by user, or exit if not

SYNOPSIS
args_confirm variableName...
DESCRIPTION
  • variableName... some existed variable names to show its value
EXAMPLES
a="correct value"
b="wrong value"
args_confirm a b
SEE_ALSO

args_print


NAME

reflect_nth_arg -- parse a string of arguments, then extract the nth argument

SYNOPSIS
reflect_nth_arg index arguments...
DESCRIPTION
  • index a number based on 1, which argument to extract
  • arguments... the string to parse, the arguments and may also including the command.
EXAMPLES
reflect_nth_arg 3 ab cdv "ha ho" ==>  "ha ho"

string="args_valid_or_read myVar '^[0-9a-z]{3,3}$' \"SIA\""
reflect_nth_arg 4 $string ==> "SIA"
SEE_ALSO

NAME

reflect_get_function_definition -- print the definition of the specified function in system

SYNOPSIS
reflect_get_function_definition functionName
DESCRIPTION
  • functionName the specified function name
EXAMPLES
reflect_get_function_definition args_confirm
SEE_ALSO

reflect_function_names_of_file


NAME

reflect_function_names_of_file -- print the function names defined in a shell script file

SYNOPSIS
reflect_function_names_of_file shellScriptFile
DESCRIPTION
  • shellScriptFile the path of shell script file
EXAMPLES
reflect_function_names_of_file $0
reflect_function_names_of_file scripts/my_script.sh
SEE_ALSO

reflect_get_function_definition


NAME

reflect_function_definitions_of_file -- print the function definitions defined in a shell script file

SYNOPSIS
reflect_function_definitions_of_file shellScriptFile
DESCRIPTION
  • shellScriptFile the path of shell script file
EXAMPLES
reflect_function_definitions_of_file $0
reflect_function_definitions_of_file scripts/my_script.sh
SEE_ALSO

reflect_get_function_definition


NAME

reflect_search_function -- search usable function by name pattern

SYNOPSIS
reflect_search_function functionNamePattern
DESCRIPTION
  • functionNamePattern the string of function name regular expression pattern
EXAMPLES
reflect_search_function args
reflect_search_function '^args_.*'
SEE_ALSO

reflect_search_variable


NAME

reflect_search_variable -- search usable variable by name pattern

SYNOPSIS
reflect_search_variable variableNamePattern
DESCRIPTION
  • variableNamePattern the string of variable name regular expression pattern
EXAMPLES
reflect_search_variable COLOR
reflect_search_variable '^COLOR'
SEE_ALSO

reflect_search_function


NAME

doc_lint_script_comment -- format the shell script, and check whether the comment is corrected man-styled

SYNOPSIS
doc_lint_script_comment shellScriptFile
DESCRIPTION

It's better format your shell script by shfmt firstly before using this function.

  • shellScriptFile the path of shell script file
EXAMPLES
shellScriptFile="src/bash-base.sh"
docker run -it --rm -v "$(pwd):/src" -w /src mvdan/shfmt -l -w "${shellScriptFile}"
doc_lint_script_comment "${shellScriptFile}"
SEE_ALSO

doc_comment_to_markdown


NAME

doc_comment_to_markdown -- convert the shell script man-styled comment to markdown file

SYNOPSIS
doc_comment_to_markdown fromShellFile toMarkdownFile
DESCRIPTION
  • fromShellFile the path of source shell script file
  • toMarkdownFile the path of destination markdown file
EXAMPLES
doc_comment_to_markdown src/bash-base.sh docs/reference.md
SEE_ALSO

doc_lint_script_comment


NAME

print_header -- print the header value with prefix ' ###' and bold font

SYNOPSIS
print_header string
DESCRIPTION
  • string the string of header title
EXAMPLES
print_header "My header1"
SEE_ALSO

print_error


NAME

print_error -- print the error message with prefix 'ERROR:' and font color red

SYNOPSIS
print_error string
DESCRIPTION
  • string the error message
EXAMPLES
print_error "my error message"
SEE_ALSO

print_header


NAME

stop_if_failed -- stop the execute if last command exit with fail code (no zero)

SYNOPSIS
stop_if_failed string
DESCRIPTION

'trap' or 'set -e' is not recommended

  • string the error message to show
EXAMPLES
rm -fr "${destProjectPath}"
stop_if_failed "ERROR: can't delete the directory '${destProjectPath}' !"
SEE_ALSO

NAME

declare_heredoc -- define a variable and init its value from heredoc

SYNOPSIS
declare_heredoc newVarName <<-EOF
...
EOF
DESCRIPTION
  • newVarName the variable name, the content of heredoc will be assigned to it
EXAMPLES
declare_heredoc records <<-EOF
record1
record2
EOF
SEE_ALSO