Executable Script Template

SolidGroundUX DocumentationSolidGroundUX
NL

Canonical executable template for SolidGroundUX scripts.

Source: target-root/usr/local/lib/solidgroundux/templates/exe-template.sh

Canonical executable template for SolidGroundUX scripts.

Public functions

Internal functions
FunctionPurposeArgumentsOutput
_framework_locatorDetermine the filesystem root of the currently executing SolidGroundUX tree—Writes fatal bootstrap errors to stderr using printf because framework UI

Description

Provides the standard structure for all executable scripts in the framework, including:

  • bootstrap resolution and framework initialization
  • optional argument parsing and configuration integration
  • metadata declaration and lifecycle hooks
  • a consistent main() entry pattern

Design principles

  • Executables are explicit: resolve, bootstrap, then run
  • Libraries never auto-execute (composition over inheritance)
  • Framework integration is opt-in and declarative
  • UI and input must be TTY-safe

Role in framework

  • Entry point pattern for all SolidGroundUX-based scripts
  • Defines how scripts integrate with sgnd-bootstrap and common libraries

Non-goals

  • Business logic implementation (provided by the script author)
  • Library behavior (handled in /common modules)

Script identity

Absolute path to the currently executing script

var: SGND_SCRIPT_FILE

Directory containing the currently executing script

var: SGND_SCRIPT_DIR

Filename of the currently executing script

var: SGND_SCRIPT_BASE

Script basename without the .sh extension

var: SGND_SCRIPT_NAME

SGND_SCRIPT_FILE

var$: SGND_SCRIPT_FILE

Absolute path to the currently executing script.

SGND_SCRIPT_DIR

var$: SGND_SCRIPT_DIR

Directory containing the currently executing script.

SGND_SCRIPT_BASE

var$: SGND_SCRIPT_BASE

Filename of the currently executing script, including extension.

SGND_SCRIPT_NAME

var$: SGND_SCRIPT_NAME

Script basename without the .sh extension; used for help and display text.

Framework integration

Optional framework libraries to source after core bootstrap

var: SGND_USING

Libraries to source from SGND_COMMON_LIB. These are loaded automatically by sgnd_bootstrap AFTER core libraries.

Example: SGND_USING=( net.sh fs.sh )

Leave empty if no extra libs are needed.

Script-specific command-line argument specification

var: SGND_ARGS_SPEC

Optional: script-specific arguments

Example: Arguments

Each entry: "name|short|type|var|help|choices"

name = long option name WITHOUT leading -- short - short option name WITHOUT leading - type = flag | value | enum var = shell variable that will be set help = help string for auto-generated --help output choices = for enum: comma-separated values (e.g. fast,slow,auto) for flag/value: leave empty

Notes:

  • -h / --help is built in, you don't need to define it here.
  • After parsing you can use: FLAG_VERBOSE, VAL_CONFIG, ENUM_MODE, ...

Optional help examples for this script

var: SGND_SCRIPT_EXAMPLES

Optional: examples for --help output. Each entry is a string that will be printed verbatim.

Example: SGND_SCRIPT_EXAMPLES=( "Example usage:" " script.sh --verbose --mode fast" " script.sh -v -m slow" )

Leave empty if no examples are needed.

Script globals participating in configuration loading# var: SGND_SCRIPT_GLOBALS - Script globals participating in configuration loading

var: SGND_SCRIPT_GLOBALS

Explicit declaration of global variables intentionally used by this script.

Purpose

  • Declares which globals are part of the script’s public/config contract.
  • Enables optional configuration loading when non-empty.

Behavior

  • If this array is non-empty, sgnd_bootstrap enables config integration.
  • Variables listed here may be populated from configuration files.
  • Unlisted globals will NOT be auto-populated.

Use this to:

  • Document intentional globals
  • Prevent accidental namespace leakage
  • Make configuration behavior explicit and predictable

Only list:

  • Variables that must be globally accessible
  • Variables that may be defined in config files

Leave empty if:

  • The script does not use configuration-driven globals

Script variables participating in persistent state

var: SGND_STATE_VARIABLES

List of variables participating in persistent state.

Purpose

  • Declares which variables should be saved/restored when state is enabled.

Behavior

  • Only used when sgnd_bootstrap is invoked with --state.
  • Variables listed here are serialized on exit (if SGND_STATE_SAVE=1).
  • On startup, previously saved values are restored before main logic runs.

Contract:

  • Variables must be simple scalars (no arrays/associatives unless explicitly supported).
  • Script remains fully functional when state is disabled.

Leave empty if:

  • The script does not use persistent state.

Script-specific exit handler list

var: SGND_ON_EXIT_HANDLERS

List of functions to be invoked on script termination.

Purpose

  • Allows scripts to register cleanup or finalization hooks.

Behavior

  • Functions listed here are executed during framework exit handling.
  • Execution order follows array order.
  • Handlers run regardless of normal exit or controlled termination.

Contract:

  • Functions must exist before exit occurs.
  • Handlers must not call exit directly.
  • Handlers should be idempotent (safe if executed once).

Typical uses:

  • Cleanup temporary files
  • Persist additional state
  • Release locks

Leave empty if:

  • No custom exit behavior is required.

SGND_STATE_SAVE

var$: SGND_STATE_SAVE

State persistence toggle used by sgnd_bootstrap when state support is enabled.

Scripts that want persistent state must:

  1. set SGND_STATE_SAVE=1
  2. call sgnd_bootstrap --state or --autostate

Local script declarations

doc$ Local script declarations Put script-local constants and defaults here, not framework configuration. Prefer local variables inside functions unless a value must be shared.

Local script functions

doc$ Local script functions Organize script-specific functions here. These functions are not reusable framework behavior; they implement the unique logic and features of the executable script.

Suggested organization:

  • Initialization
  • Local script helpers
  • Layout and display helpers
  • Input handling
  • Business logic
  • Validation
  • Reporting
  • Cleanup