Skip to content

Repository files navigation

L_lib.sh

my labrador dog

Labrador Bash library. Collection of functions and libraries that I deem useful for working with Bash.

Installation

The library is one file. Download the latest release from GitHub and put in your PATH:

mkdir -vp ~/.local/bin/
wget -O ~/.local/bin/L_lib.sh https://github.com/Kamilcuk/L_lib/releases/download/v2.1.0/L_lib.sh
export PATH=~/.local/bin:$PATH

Pip

pip install L_lib

Basher

basher install Kamilcuk/L_lib

Usage

You can use the library in scripts with:

. L_lib.sh -s

Unless -n, sourcing the library will enable extglob and patsub_replacement and, if set -e is set and there is no ERR trap, it will also register a ERR trap that will print a nice traceback on unhandled error.

For example, a simple script using the library could look like:

#!/usr/bin/env bash
. L_lib.sh -s
L_log "Starting script"
L_info "This is an info message"

You can test the library ad-hoc:

bash <(wget -qO- https://github.com/Kamilcuk/L_lib/releases/download/v2.1.0/L_lib.sh) L_setx L_log 'Hello world'

Commonly used functions

Here are the functions most frequently used throughout the L_lib codebase (including scripts and tests):

  • L_finally - Execute an action on EXIT, RETURN, or signal.
  • L_argparse - Parse command line arguments.
  • L_log - Log a message with a specified level.
  • L_handle_v_scalar - Handle the -v <var> option to store results in a variable.
  • L_info - Log an informational message.
  • L_panic - Print error with traceback and exit.
  • L_xargs - Run parallel background tasks with controlled concurrency.
  • L_foreach - Iterate over arrays with automatic index tracking.
  • L_with_tmpfile_into - Execute a command with its output stored in a temporary file.
  • L_with_process_into - Execute a command and store its stdout/stderr in variables.

Features

Below is a selection of the library's features. The library contains much more.

Talk to me

Kindly feel free to have conversations and ask questions on GitHub discussion.

Report bugs using GitHub issue.

Contributing

Contributions are welcome! You can run the tests locally with make test or check static analysis with make shellcheck. Please submit pull requests to the main repository.

Conventions

  • L_* prefix for public symbols.
  • _L_* prefix for private symbols, including local variables in functions taking a name-reference.
  • Upper case used for global scope read-only variables.
  • Lower case used for functions and user mutable variables.
  • Snake case for everything.
  • The option -v <var> is used to store the result in a variable instead of printing it.
    • This follows the convention of printf -v <var>.
    • Without the -v option, the function outputs the elements on lines to standard output.
    • Associated function with _vL_RET suffix store the result in a hardcoded scratch variable L_RET.
  • Return 64 ($L_EX_USAGE) on usage error, return 124 ($L_EX_TIMEOUT) on timeout.

License

GPL-3.0

About

Bash library for day to day tasks and scripting

Resources

Stars

7 stars

Watchers

1 watching

Forks

Releases

Sponsor this project

Packages

Used by

Contributors

Languages