args_parse – parse the script argument values to positional variable names, process firstly the optional param help(-h) / quiet(-q) if existed
args_parse $# "$@" positionalVarName...
args_parse $# "$@" newVar1 newVar2 newVar3
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
args_valid_or_select valueVarName arrayVarName prompt
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"
args_valid_or_select_pipe, args_valid_or_read, args_valid_or_default
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
args_valid_or_select_pipe valueVarName strValidValues prompt
strValidValues values joined by pipe ‘ | ’ |
sel="abc"
args_valid_or_select_pipe sel "a|ab|d" "which value"
args_valid_or_select, args_valid_or_select_args, args_valid_or_read, args_valid_or_default
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
args_valid_or_select_args valueVarName prompt arrayElement1 arrayElement2 ...
sel="abc"
args_valid_or_select_args sel "which value" "a" "ab" "d"
args_valid_or_select, args_valid_or_select_pipe, args_valid_or_read, args_valid_or_default
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
args_valid_or_read valueVarName strRegExp prompt [proposedValue]
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}"
args_valid_or_select, args_valid_or_select_args, args_valid_or_select_pipe, args_valid_or_default
args_valid_or_default – description of the optional params, value will fallback to empty or default if it no match the regular expression.
args_valid_or_default valueVarName strRegExp prompt [defaultValue]
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’.
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}"
args_valid_or_select, args_valid_or_select_args, args_valid_or_select_pipe, args_valid_or_read
array_join – join an array to string using delimiter string
array_join delimiter arrayVarName
myArry=(" a " " b c ")
array_join '|' myArry ==> " a | b c "
string_split_to_array, array_describe, array_from_describe
array_describe – convert the array to its string representation
array_describe arrayVarName
myArray=("a" "b")
array_describe myArray ==> ([0]='a' [1]='b')
string_split_to_array, array_join, array_from_describe
array_from_describe – restore the array from its string representation, then assign it to a variable name
array_from_describe newArrayVarName [string]
array_from_describe myNewArray "([0]='a' [1]='b')"
array_from_describe myNewArray < fileNameContentString
string_split_to_array, array_join, array_describe
array_contains – exit success code 0 if array contains element, fail if not.
array_contains arrayVarName [seekingElement]
arr=("a" "b" "c" "ab" "f" "g")
array_contains arr "ab"
echo "ab" | array_contains arr
array_remove, array_in
array_in – exit success code 0 if first item is in the array of the rest arguments, fail if not.
array_in seekingElement arrayElement1 arrayElement2 arrayElement3 ...
arr=("a" "b" "c" "ab" "f" "g")
array_in "a b" "a" "b" "c" "a b" "f" "g"
array_remove, array_contains
array_sort – sort the elements of array, save the result to original variable name
array_sort arrayVarName
myArray=('aa' 'bb' 'aa')
array_sort myArray ==> ([0]='aa' [1]='aa' [2]='bb')
array_sort_distinct
array_sort_distinct – remove the duplicated elements of array, sort and save the result to original variable name
array_sort_distinct arrayVarName
myArray=('aa' 'bb' 'aa')
array_sort_distinct myArray ==> ([0]='aa' [1]='bb')
array_sort
array_length – return the number of elements of array
array_length arrayVarName
myArray=('aa' 'bb' 'aa')
array_length myArray ==> 3
array_reset_index – reset the indexes of array to the sequence 0,1,2…, save the result to original variable name
array_reset_index arrayVarName
myArray=([2]='a' [5]='c' [11]='dd')
array_reset_index myArray ==> ([0]='a' [1]='c' [2]='dd')
array_equals – test if the elements of 2 array are equal, ignore the array index
array_equals arrayVarName1 arrayVarName2 [ignoreOrder] [ignoreDuplicated]
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
array_intersection – calcul the intersection of 2 arrays, and save the result to a new variable
array_intersection arrayVarName1 arrayVarName2 newArrayVarName [ignoreOrderAndDuplicated]
myArray1=('aa' [3]='bb' 'aa' 'cc')
myArray2=('aa' 'aa' 'dd' 'bb')
array_intersection myArray1 myArray2 newArray
array_intersection myArray1 myArray2 newArray false
array_subtract, array_union
array_subtract – calcul the subtract of 2 arrays, and save the result to a new variable
array_subtract arrayVarName1 arrayVarName2 newArrayVarName [ignoreOrderAndDuplicated]
myArray1=('aa' [3]='bb' 'aa' 'cc')
myArray2=('aa' 'aa' 'dd' 'bb')
array_subtract myArray1 myArray2 newArray
array_subtract myArray1 myArray2 newArray false
array_intersection, array_union
array_union – calcul the union of 2 arrays, and save the result to a new variable
array_union arrayVarName1 arrayVarName2 newArrayVarName [ignoreOrderAndDuplicated]
myArray1=('aa' [3]='bb' 'aa' 'cc')
myArray2=('aa' 'aa' 'dd' 'bb')
array_union myArray1 myArray2 newArray
array_union myArray1 myArray2 newArray false
array_intersection, array_union
array_append – append some elements to original array
array_append arrayVarName element...
myArray=()
array_append myArray "ele ment1" "ele ment2"
array_remove
array_remove – remove the element from the original array
array_remove arrayVarName element
arr=("a" "b" "c" "ab" "f" "g")
array_remove arr "ab"
array_contains, array_append
array_clone – clone an array, including index/order/duplication/value, and assign the result array to a new variable name
array_clone arrayVarName newArrayVarName
arr=(" a " " b c ")
array_clone arr newArray
array_map – apply the specified map operation on each element of array, and assign the result array to a new variable name
array_map arrayVarName pipedOperators [newArrayVarName]
pipedOperators a string of operations, if multiple operations will be apply on each element, join them by pipe ‘ | ’ |
arr=(" a " " b c ")
array_map arr "string_trim | wc -m | string_trim" newArray
array_filter – filter the elements of an array, and assign the result array to a new variable name
array_filter arrayVarName regExp [newArrayVarName]
arr=("NAME A" "NAME B" "OTHER")
array_filter arr 'NAME' newArray
doc_lint_script_comment – format the shell script, and check whether the comment is corrected man-styled
doc_lint_script_comment shellScriptFile
It’s better format your shell script by shfmt
firstly before using this function.
shellScriptFile="src/reflection.sh"
docker run -it --rm -v "$(pwd):/src" -w /src mvdan/shfmt -l -w "${shellScriptFile}"
doc_lint_script_comment "${shellScriptFile}"
doc_comment_to_markdown
doc_comment_to_markdown – convert the shell script man-styled comment to markdown file
doc_comment_to_markdown fromShellFile toMarkdownFile
doc_comment_to_markdown src/reflection.sh docs/references.md
doc_lint_script_comment
print_info – if LOG_LEVEL<=$LOG_LEVEL_DEBUG, print the information message with font color gray
print_info [string]
print_info "my message"
print_header, print_error, print_success, print_warn, print_args, print_info
print_info – if LOG_LEVEL<=$LOG_LEVEL_INFO, print the information message with font color default
print_info [string]
print_info "my message"
print_header, print_error, print_success, print_warn, print_args, print_debug
print_warn – if LOG_LEVEL<=$LOG_LEVEL_WARN, print the warning message with prefix ‘WARN:’ and font color yellow
print_warn [string]
print_warn "my message"
print_header, print_error, print_success, print_info, print_args, print_debug
print_error – if LOG_LEVEL<=$LOG_LEVEL_ERROR, print the error message with prefix ‘ERROR:’ and font color red
print_error [string]
print_error "my error message"
print_header, print_success, print_warn, print_info, print_args, print_debug
print_success – if LOG_LEVEL<=$LOG_LEVEL_WARN, print the success message with prefix ‘OK:’ and font color green
print_success [string]
print_success "my message"
print_header, print_error, print_warn, print_info, print_args, print_debug
print_args – if LOG_LEVEL<=$LOG_LEVEL_WARN, show the name and value of variables
print_args variableName...
var1="value 1"
var2="value 2"
print_args var1 var2
print_header, print_error, print_success, print_warn, print_info, print_debug
print_header – if LOG_LEVEL<=$LOG_LEVEL_ERROR, print the header value with prefix ‘ ###’ and bold font
print_header [string]
print_header "My header1"
print_error, print_success, print_warn, print_info, print_args, print_debug
prc_filter_by_port – list the process with port listened, not with sudo
prc_filter_by_port [port]
prc_filter_by_port 9090
prc_kill_by_port
prc_kill_by_port – kill the process who listening on the specific port, not with sudo
prc_kill_by_port port [signal]
prc_kill_by_port 9090
prc_filter_by_port
prc_filter_by_cmd – print out the proccess with the filter of command and its arguments, not with sudo
prc_filter_by_cmd [command]
prc_filter_by_cmd node
prc_kill_by_cmd
prc_kill_by_cmd – search the process by the command and arguments, and kill it, not with sudo
prc_kill_by_cmd command [signal]
prc_kill_by_cmd my-app
prc_filter_by_cmd
reflect_nth_arg – parse a string of arguments, then extract the nth argument
reflect_nth_arg index arguments...
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"
reflect_get_function_definition – print the definition of the specified function in system
reflect_get_function_definition functionName
reflect_get_function_definition confirm_to_continue
reflect_function_names_of_file
reflect_function_names_of_file – print the function names defined in a shell script file
reflect_function_names_of_file shellScriptFile
reflect_function_names_of_file $0
reflect_function_names_of_file scripts/my_script.sh
reflect_get_function_definition
reflect_function_definitions_of_file – print the function definitions defined in a shell script file
reflect_function_definitions_of_file shellScriptFile
reflect_function_definitions_of_file $0
reflect_function_definitions_of_file scripts/my_script.sh
reflect_get_function_definition
reflect_search_function – search usable function by name pattern
reflect_search_function functionNamePattern
reflect_search_function args
reflect_search_function '^args_.*'
reflect_search_variable
reflect_search_variable – search usable variable by name pattern
reflect_search_variable variableNamePattern
reflect_search_variable COLOR
reflect_search_variable '^COLOR'
reflect_search_function
string_trim – remove the white chars from prefix and suffix
string_trim [string]
string_trim " as fd "
string_trim < logfile
echo " add " | string_trim
string_repeat – make a string by repeat n times of a token string
string_repeat string [nbTimes]
string_repeat 'abc' 5
echo 5 | string_repeat 'abc'
string_length – return the string length
string_length [string]
string_length " as fd "
string_length < logfile
echo " add " | string_length
string_is_empty – exit success code 0 if the string is empty
string_is_empty [string]
string_is_empty " as fd "
string_is_empty < logfile
echo " add " | string_is_empty
string_length
string_revert – revert the characters of a string
string_revert [string]
string_revert 'aBc'
echo 'aBc' | string_revert
string_upper – convert all characters to upper case
string_upper [string]
string_upper 'abc'
echo 'abc' | string_upper
string_upper_first, string_lower
string_lower – convert all characters to lower case
string_lower [string]
string_lower 'aBc'
echo 'aBc' | string_lower
string_upper, string_upper_first
string_upper_first – convert the first characters to upper case, and the others to lower case
string_upper_first [string]
string_upper_first 'aBc'
echo 'aBc' | string_upper_first
string_lower, string_upper
string_sub – extract a part of string and return
string_sub startIndex subStringLength [string]
string_sub -5 -1 " as fd "
string_sub 3 5 < temp_file.txt
echo ' as fd ' | string_sub 2 4
string_match – test if the string match the regular expression
string_match regExp [string]
string_match 'name;+' "name;name;"
string_index_first
escape_sed – escape preserved char of regex, normally for preprocessing of sed token.
escape_sed string
escape_sed 'a$'
string_replace
string_replace – replace literally the token string to new string, not support regular expression
string_replace tokenString newString [string]
string_replace 'a' 'b' 'aaa' ==> 'bbb'
string_replace '$' 'b' 'a$a' ==> 'aba'
string_replace '\*' 'b' 'a*a' ==> 'aba'
escape_sed, string_replace_regex
string_replace_regex – replace the token string to new string, support regular expression
string_replace_regex tokenString newString [string]
string_replace_regex 'a*' 'b' 'a*a' ==> 'b*b'
string_replace_regex 'a*' 'b' "aaa" ==> 'b'
string_replace_regex '*' 'b' 'a*a' ==> 'aba'
string_replace
string_index_first – return the positive index of first place of token in string, -1 if not existed
string_index_first tokenString [string]
string_index_first "s f" " as fd "
string_index_first "token" < logfile
echo " add " | string_index_first "token"
string_before_first, string_after_first
string_before_first – find the first index of token in string, and return the sub string before it.
string_before_first tokenString [string]
string_before_first "s f" " as fd "
string_before_first "str" < logfile
echo " add " | string_before_first "dd"
string_index_first, string_after_first
string_after_first – find the first index of token in string, and return the sub string after it.
string_after_first tokenString [string]
string_after_first "s f" " as fd "
string_after_first "str" < logfile
echo " add " | string_after_first "dd"
string_index_first, string_before_first
string_split_to_array – split a string to array by a delimiter character, then assign the array to a new variable name
string_split_to_array tokenString [newArrayVarName] [string]
str="a|b|c"
string_split_to_array '|' newArray "$str"
branchesToSelectString=$(git branch -r --list 'origin/*')
string_split_to_array $'
' branchesToSelectArray "${branchesToSelectString}"
array_join, array_describe, array_from_describe, string_pick_to_array
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
string_pick_to_array startTokenString endTokenString [newArrayVarName] [string]
str="[{age:12},{age:15},{age:16}]"
string_pick_to_array '{age:' '}' newArray "$str"
array_join, array_describe, array_from_describe, string_split_to_array
stop_if_failed – stop the execute if last command exit with fail code (no zero)
stop_if_failed string
‘trap’ or ‘set -e’ is not recommended
rm -fr "${destProjectPath}"
stop_if_failed "ERROR: can't delete the directory '${destProjectPath}' !"
confirm_to_continue
confirm_to_continue – show the name and value of variables, and continue execute if confirm_to_continueed by user, or exit if not
confirm_to_continue variableName...
a="correct value"
b="wrong value"
confirm_to_continue a b
print_args, stop_if_failed
wait_for – wait the subject predicate to be true before continue
wait_for predicate [subject] [interval]
wait_for 'test -f /tmp/output.txt' 'file existed' 3
confirm_to_continue, stop_if_failed
declare_heredoc – define a variable and init its value from heredoc
declare_heredoc newVarName <<-EOF
...
EOF
declare_heredoc records <<-EOF
record1
record2
EOF