bash-base

Functions:

Usage:

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
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
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, args_valid_or_default


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
EXAMPLES
sel="abc"
args_valid_or_select_pipe sel "a|ab|d" "which value"
SEE_ALSO

args_valid_or_select, args_valid_or_select_args, args_valid_or_read, args_valid_or_default


NAME

args_valid_or_select_args – 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_args valueVarName prompt arrayElement1 arrayElement2 ...
DESCRIPTION
EXAMPLES
sel="abc"
args_valid_or_select_args sel "which value" "a" "ab" "d"
SEE_ALSO

args_valid_or_select, args_valid_or_select_pipe, args_valid_or_read, args_valid_or_default


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
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_args, args_valid_or_select_pipe, args_valid_or_default


NAME

args_valid_or_default – description of the optional params, value will fallback to empty or default if it no match the regular expression.

SYNOPSIS
args_valid_or_default valueVarName strRegExp prompt [defaultValue]
DESCRIPTION

the optional params should be set in OS environment like ‘export optional_variable=value && ./my_script.sh’, or be placed before the $0 like ‘optionalVariable1=value1 optionalVariable2=value2 ./my_script.sh’.

EXAMPLES
args_valid_or_default destProjectSIA '^[0-9a-z]{3,3}$' "SIA (lowercase, 3 chars)"
args_valid_or_default destProjectIRN '^[0-9]{5,5}$' "IRN (only the 5 digits)"
args_valid_or_default destRootPackage '^.+$' "Destination root package" "${defaultDestRootPackage}"
SEE_ALSO

args_valid_or_select, args_valid_or_select_args, args_valid_or_select_pipe, args_valid_or_read


NAME

array_join – join an array to string using delimiter string

SYNOPSIS
array_join delimiter arrayVarName
DESCRIPTION
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
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
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
EXAMPLES
arr=("a" "b" "c" "ab" "f" "g")
array_contains arr "ab"
echo "ab" | array_contains arr
SEE_ALSO

array_remove, array_in


NAME

array_in – exit success code 0 if first item is in the array of the rest arguments, fail if not.

SYNOPSIS
array_in seekingElement arrayElement1 arrayElement2 arrayElement3 ...
DESCRIPTION
EXAMPLES
arr=("a" "b" "c" "ab" "f" "g")
array_in "a b" "a" "b" "c" "a b" "f" "g"
SEE_ALSO

array_remove, array_contains


NAME

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

SYNOPSIS
array_sort arrayVarName
DESCRIPTION
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
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
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
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
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
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
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
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
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
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
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
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
EXAMPLES
arr=("NAME A" "NAME B" "OTHER")
array_filter arr 'NAME' newArray
SEE_ALSO

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.

EXAMPLES
shellScriptFile="src/reflection.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
EXAMPLES
doc_comment_to_markdown src/reflection.sh docs/references.md
SEE_ALSO

doc_lint_script_comment


NAME

print_info – if LOG_LEVEL<=$LOG_LEVEL_DEBUG, print the information message with font color gray

SYNOPSIS
print_info [string]
DESCRIPTION
EXAMPLES
print_info "my message"
SEE_ALSO

print_header, print_error, print_success, print_warn, print_args, print_info


NAME

print_info – if LOG_LEVEL<=$LOG_LEVEL_INFO, print the information message with font color default

SYNOPSIS
print_info [string]
DESCRIPTION
EXAMPLES
print_info "my message"
SEE_ALSO

print_header, print_error, print_success, print_warn, print_args, print_debug


NAME

print_warn – if LOG_LEVEL<=$LOG_LEVEL_WARN, print the warning message with prefix ‘WARN:’ and font color yellow

SYNOPSIS
print_warn [string]
DESCRIPTION
EXAMPLES
print_warn "my message"
SEE_ALSO

print_header, print_error, print_success, print_info, print_args, print_debug


NAME

print_error – if LOG_LEVEL<=$LOG_LEVEL_ERROR, print the error message with prefix ‘ERROR:’ and font color red

SYNOPSIS
print_error [string]
DESCRIPTION
EXAMPLES
print_error "my error message"
SEE_ALSO

print_header, print_success, print_warn, print_info, print_args, print_debug


NAME

print_success – if LOG_LEVEL<=$LOG_LEVEL_WARN, print the success message with prefix ‘OK:’ and font color green

SYNOPSIS
print_success [string]
DESCRIPTION
EXAMPLES
print_success "my message"
SEE_ALSO

print_header, print_error, print_warn, print_info, print_args, print_debug


NAME

print_args – if LOG_LEVEL<=$LOG_LEVEL_WARN, show the name and value of variables

SYNOPSIS
print_args variableName...
DESCRIPTION
EXAMPLES
var1="value 1"
var2="value 2"
print_args var1 var2
SEE_ALSO

print_header, print_error, print_success, print_warn, print_info, print_debug


NAME

print_header – if LOG_LEVEL<=$LOG_LEVEL_ERROR, print the header value with prefix ‘ ###’ and bold font

SYNOPSIS
print_header [string]
DESCRIPTION
EXAMPLES
print_header "My header1"
SEE_ALSO

print_error, print_success, print_warn, print_info, print_args, print_debug


NAME

prc_filter_by_port – list the process with port listened, not with sudo

SYNOPSIS
prc_filter_by_port [port]
DESCRIPTION
EXAMPLES
prc_filter_by_port 9090
SEE_ALSO

prc_kill_by_port


NAME

prc_kill_by_port – kill the process who listening on the specific port, not with sudo

SYNOPSIS
prc_kill_by_port port [signal]
DESCRIPTION
EXAMPLES
prc_kill_by_port 9090
SEE_ALSO

prc_filter_by_port


NAME

prc_filter_by_cmd – print out the proccess with the filter of command and its arguments, not with sudo

SYNOPSIS
prc_filter_by_cmd [command]
DESCRIPTION
EXAMPLES
prc_filter_by_cmd node
SEE_ALSO

prc_kill_by_cmd


NAME

prc_kill_by_cmd – search the process by the command and arguments, and kill it, not with sudo

SYNOPSIS
prc_kill_by_cmd command [signal]
DESCRIPTION
EXAMPLES
prc_kill_by_cmd my-app
SEE_ALSO

prc_filter_by_cmd


NAME

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

SYNOPSIS
reflect_nth_arg index arguments...
DESCRIPTION
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
EXAMPLES
reflect_get_function_definition confirm_to_continue
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
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
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
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
EXAMPLES
reflect_search_variable COLOR
reflect_search_variable '^COLOR'
SEE_ALSO

reflect_search_function


NAME

string_trim – remove the white chars from prefix and suffix

SYNOPSIS
string_trim [string]
DESCRIPTION
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
EXAMPLES
string_repeat 'abc' 5
echo 5 | string_repeat 'abc'
SEE_ALSO

NAME

string_length – return the string length

SYNOPSIS
string_length [string]
DESCRIPTION
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
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
EXAMPLES
string_revert 'aBc'
echo 'aBc' | string_revert
SEE_ALSO

NAME

string_upper – convert all characters to upper case

SYNOPSIS
string_upper [string]
DESCRIPTION
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
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
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
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
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
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
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
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
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
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
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
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
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

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

EXAMPLES
rm -fr "${destProjectPath}"
stop_if_failed "ERROR: can't delete the directory '${destProjectPath}' !"
SEE_ALSO

confirm_to_continue


NAME

confirm_to_continue – show the name and value of variables, and continue execute if confirm_to_continueed by user, or exit if not

SYNOPSIS
confirm_to_continue variableName...
DESCRIPTION
EXAMPLES
a="correct value"
b="wrong value"
confirm_to_continue a b
SEE_ALSO

print_args, stop_if_failed


NAME

wait_for – wait the subject predicate to be true before continue

SYNOPSIS
wait_for predicate [subject] [interval]
DESCRIPTION
EXAMPLES
wait_for 'test -f /tmp/output.txt' 'file existed' 3
SEE_ALSO

confirm_to_continue, stop_if_failed


NAME

declare_heredoc – define a variable and init its value from heredoc

SYNOPSIS
declare_heredoc newVarName <<-EOF
...
EOF
DESCRIPTION
EXAMPLES
declare_heredoc records <<-EOF
record1
record2
EOF
SEE_ALSO