Skip to content

utilities

utilities

Various self contained functions that could be separate programs.

L_table

Make a table

Example

$ L_table -R1-2 "name1 name2 name3" "a b c" "d e f"
name1 name2 name3
    a     b c
    d     e f

Options:

  • -v <var> Store the output in variable instead of printing it.
  • -s <separator> IFS column separator to use. Default: space or tab.
  • -o <str> Output separator to use
  • -R <list[int]> Right align columns with these indexes
  • -h Print this help and return 0.

Argument: $@ Lines to print, joined and separated by newline.

Shellcheck disable= SC1105 SC2201 SC2102 SC2035 SC2211 SC2283 SC2094

L_parse_range_list

Parse cut range list into an array.

Each LIST is made up of one range, or many ranges separated by commas. Selected input is written in the same order that it is read, and is written exactly once. Each range is one of: N N'th byte, character or field, counted from 1 N- from N'th byte, character or field, to end of line N-M from N'th to M'th (included) byte, character or field -M from first to M'th (included) byte, character or field

Example

$ L_parse_range_list 100 1-4,3-5
1
2
3
4
5
$ L_parse_range_list -v tmp 100 '1-4 3-5'
$ echo "${tmp[@]}"
1 2 3 4 5
$ if L_args_contain 3 "${tmp[@]}"; then echo "yes"; else echo "no"; fi
yes
$ if L_args_contain 7 "${tmp[@]}"; then echo "yes"; else echo "no"; fi
no

Option: -v <var> Store the output in variable instead of printing it.

Arguments:

  • $1 max number of fields
  • $2 list of fields

L_parse_range_list_v

L_pretty_print

Prints values with declare, but array values are on separate lines.

Options:

  • -p <str> Prefix each line with this prefix
  • -v <var> Store the output in variable instead of printing it.
  • -w <int> Set terminal width. This modifies compact output line wrapping.
  • -n Enable pretty printing nested arrays
  • -C Disable compact output for arrays, i.e. each key is on separate line.
  • -h Print this help and return 0.
  • -c Compact output.

Argument: $@ variable names to pretty print

L_argskeywords

Parse python-like positional and keyword arguments format.

The difference to python is that @ is used instead of *, becuase * triggers filename expansion. An argument -- signifies end of arguments definition and start of arguments to parse.

Example

range() {
   local start stop step
    L_argskeywords start stop step=1 -- "$@" || return 2
    for ((; start < stop; start += stop)); do echo "$start"; done
}
range start=1 stop=6 step=2
range 1 6

max() {
   local arg1 arg2 args key
   L_argskeywords arg1 arg2 @args key='' -- "$@" || return 2
   ...
}
max 1 2 3 4

int() {
   local string base
   L_argskeywords string / base=10 -- "$@" || return 2
   ...
}
int 10 7 # error
int 10 base=7

Options:

  • -A <var> Instead of storing in variables, store values in specified associative array with variables as key.
  • -M Use L_map instead of associative array, for @@kwargs and -A option. Usefull for older Bash.
  • -E Exit on error
  • -e <str> Prefix error messages with this prefix. Default: "${FUNCNAME[1]}:L_argskeywords:"
  • -h Print this help and return 0.

Arguments:

  • $@ Python arguments format specification
  • $2 --
  • $@ Arguments to parse

See:

L_version_cmp

Compare version numbers.

Arguments:

  • $1 str one version
  • $2 str one of: -lt -le -eq -ne -gt -ge '<' '<=' '==' '!=' '>' '>=' '~='
  • $3 str second version
  • [$4] int accuracy, how many at max elements to compare? By default up to 3.

Shellcheck disable= SC2053

See: https://peps.python.org/pep-0440/