This is a rough guide for adding and integrating new units (sub-libraries) to a base Bashy-lib installation.
-
Copy the
scripts
directory of this project. -
Put your scripts in one of two places:
- A unit (sub-library) directory for your project in
scripts/lib
. - Directly in
scripts
(if they don't need to be called by other scripts).
- A unit (sub-library) directory for your project in
-
Put a non-executable file called
_init.sh
in every directory where a script lives. This file is included by your scripts and serves to link them up to the main library. -
Put the following line at the top of every script:
. "$(dirname "$(readlink -f "$0")")/_init.sh" || exit "$?"
project-base-directory/
scripts/
_init.sh -- boilerplate init file
top-level-script
top-level-script
ubik -- general library caller (copy from this project)
lib/
_init.sh -- boilerplate (mostly) init file
bashy-core/ -- copy of directory from this project
other-lib/ -- copy of other library (from this project or elsewhere)
my-project/
_init.sh -- boilerplate init file
_prereqs -- unit-specific prerequisites checker (optional)
_setup.sh -- unit-specific setup (optional)
project-script
project-script
project-subcommand-dir/
_init.sh -- boilerplate init file
_run -- default subcommand script (optional)
subcommand-script
subcommand-script
subsub-dir/
_run -- default subcommand script (optional)
subcommand-script
project-subcommand-dir/
_init.sh -- boilerplate init file
_run -- default subcommand script (optional)
subcommand-script
subcommand-script
-
Pick a name for your project's "script library" directory, typically at the top level of your project.
scripts
andbin
are good choices.The rest of these instructions assume you picked
scripts
, for ease of exposition. Adjust accordingly. -
Pick a symbolic name for your project / product, e.g. that can (and will) be used as a directory name.
The rest of these instructions assume you named your project
my-project
. Adjust accordingly. -
Copy the items from the
scripts
directory in this project, that you are interested in using, intoscripts
in your project. At a minimum, you need to include thelib/bashy-core
andlib/_init.sh
. The files directly inscripts
(_init.sh
andubik
) are needed if you want to expose library scripts "publicly" inscripts
(at least, in the standard way supported by this project).Note:
lib/_init.sh
file will need to be adjusted ifscripts
is not directly under your project's base directory. -
Make a directory for your own script sub-library,
scripts/lib/my-project
. -
Create a file called
scripts/lib/my-project/_init.sh
, to hook up your project's script sub-library tobashy-core
. The file should just contain this:. "${BASH_SOURCE[0]%/lib/*}/lib/_init.sh" || return "$?"
-
Create one or more scripts in
scripts/lib/my-project
, or directly inscripts
. At the top of each script, include the following:. "$(dirname "$(readlink -f "$0")")/_init.sh" || exit "$?"
Note: Scripts directly in
scripts
should generally not be called from other scripts. See below about "exposing" a script in your unit for direct "public" calling. -
Create one or more subcommand directories in
scripts/lib/my-project
. Add an_init.sh
file to it (same as in step 5), and one or more scripts or subcommand directories (same as step 6 or this step). -
To expose a script in a unit for direct "public" usage, create a script with the same name as the script in the top-level
scripts
directory, with the following contents, which causes it to call through to the unit script:#!/bin/bash . "$(dirname "$(readlink -f "$0")")/_init.sh" || exit "$?" lib "$(this-cmd-name)" "$@"
Note: The files named with a .sh
suffix are not supposed to be marked
executable (chmod +x ...
). These are include files.
Sometimes it makes sense to define subprojects as separate directories within a project. In such cases, you may still want to have those subprojects' scripts be available in the top level project for interactive or script-available use (or both). Bashy-lib makes this arrangement possible via symlinking either per se or via "reified link" files (a text file containing the path, which may be preferable because of source control concerns). Relative reified links are taken to use the main project base directory as the base directory of the link.
For example, let's say you have a directory my-subproject
under your main
project, which has its own scripts
directory. Using symlinks per se:
$ cd my-project/scripts/lib/my-project # Your project's main scripts.
$ ln -s ../../../my-subproject/scripts my-subproject
Or using a reified link file:
$ cd my-project
$ echo 'my-subproject/scripts' > scripts/lib/my-subproject.link
With that, you can now say ubik my-subproject ...
or (in scripts) lib my-subproject ...
.
Copyright 2022-2025 the Bashy-lib Authors (Dan Bornstein et alia).
SPDX-License-Identifier: Apache-2.0