Generic bash library functions for managing shell options (shopt
), timers, processes and file descriptors.
Contains common usage functions, such as some process, shopt, file descriptor and timer management.
It contains the following classes:
- main
- shopt
- process
- env
- fd
- var
- timer
- flag
Use the command module.doc <function_name>
to see the documentation for a function (see an example)
- _MAIN__KILL_PROCESS_WAIT_INTERVAL (Number)[default: 0.1]: Seconds to wait between checks to test whether a process has been successfully killed
- _SETTINGS__HASH[SOURCED] (Bool): Is current file sourced? This flag is automatically set when the module is loaded
- _SETTINGS__HASH[PIPED] (Bool): Is current file piped to another command? This flag is automatically set when the module is loaded
- _SETTINGS__HASH[INTERACTIVE] (Bool): Is the current process running in an interactive shell? This flag is automatically set when the module is loaded
- _SETTINGS__HASH[CHROOTED] (Bool): Is current process chrooted? This flag is set when calling
main.is-chroot()
- _SETTINGS__HASH[WINDOWS] (Bool): Is current O.S. Windows? This flag is set when calling
main.is-windows()
- True 0
- False 1
- _MAIN__RAW_SCRIPTNAME (String): Calling script path, raw and not normalized: as seen by the shell through BASH_SOURCE variable
- _MAIN__SCRIPTPATH (String): Calling script path after any possible link resolution
- _MAIN__SCRIPTNAME (String): Calling script real name (after any possible link resolution)
- _MAIN__SCRIPTDIR (String): Absolute path where reside the calling script, after any possible link resolution
- _MAIN__GIT_PATH (String): Root path of Git for Windows environment: it's set when calling
main.is-windows()
- main_dereference-alias_()
- main_is-windows()
- main_is-chroot()
- main_set-script-path-info()
- main_is-piped()
- shopt_backup()
- shopt_restore()
- timer_start()
- timer_elapsed()
- fd_get_()
- process_is-child()
- process_kill()
- env_PATH_append-item()
- env_PATH_prepend-item()
- file_mkfifo_()
- command_stdout_()
- var_assign()
- var_is-set()
- settings_is-enabled()
- settings_is-disabled()
- settings_enable()
- settings_disable()
- settings_set()
- settings_get_()
Dereference shell aliases: return the name of the function to which an alias point to, resolving it recursively if needed
- main.dereference-alias_
- $1 (String): Name of alias to dereference
- String Name of function to which provided alias point to
$ alias alias1="func1"
$ alias alias2="alias1"
$ main.dereference-alias_ alias2
# return __="func1"
Check whether the current environment is Windows, testing if uname -a
return a string starting with MINGW
.
Store the result $True or $False in the flag _SETTINGS__HASH[WINDOWS].
- Standard (0 for true, 1 for false)
- main.is-windows
$ uname -a
MINGW64_NT-6.1 chiller2 2.11.2(0.329/5/3) 2018-11-10 14:38 x86_64 Msys
$ main.is-windows
# statuscode = 0
Check whether the script is chroot'ed, and store the value $True or $False in flag $_SETTINGS__HASH[CHROOTED].
- main.is-chroot
- Standard (0 for true, 1 for false)
main.is-chroot
Set the current script path and the current script directory to the global variables _MAIN__SCRIPTPATH
and _MAIN__SCRIPTDIR
.
- main.set-script-path-info
$ main.set-script-path-info
$ echo _MAIN__SCRIPTPATH=$_MAIN__SCRIPTPATH
_MAIN__SCRIPTPATH=/usr/local/src/script.sh
$ echo _MAIN__SCRIPTDIR=$_MAIN__SCRIPTDIR
_MAIN__SCRIPTDIR=/usr/local/src
Check if the script is piped.
- main.is-piped
- Standard (0 for true, 1 for false)
Backup the provided shopt options.
- shopt.backup
- ... (String): Options to be backed up
$ shopt -p expand_aliases
shopt -s expand_aliases
$ shopt.backup expand_aliases extdebug
$ shopt -u expand_aliases
$ shopt -p expand_aliases
shopt -u expand_aliases
$ shopt.restore expand_aliases extdebug
$ shopt -p expand_aliases
shopt -s expand_aliases
Restore the provided shopt options backuped up by the previously called shopt.backup
function.
- shopt.restore
- ... (String): Options to be restored
$ shopt -p expand_aliases
shopt -s expand_aliases
$ shopt.backup expand_aliases extdebug
$ shopt -u expand_aliases
$ shopt -p expand_aliases
shopt -u expand_aliases
$ shopt.restore expand_aliases extdebug
$ shopt -p expand_aliases
shopt -s expand_aliases
Start a timer
- timer.start
- $1 (String)[default: _]: Name of timer
Return the seconds elapsed for the provided timer
- timer.elapsed
- $1 (String)[default: _]: Name of timer
- Return the elapsed seconds for the timer
$ timer.start timer1
$ sleep 5
$ timer.elapsed timer1
# return __=5
Get the first file descriptor number available.
- hfd.get_
Function has no arguments.
- File descriptor number
Test if process with provided PID is a child of the current process.
Check if a process with provided PID exists.
- process.exists
- $1 (Number): PID of the process
- 0 if process exists, 1 otherwise
- process.is-child
- $1 (Number): PID of the process
- 0 if process is a child process, 1 otherwise
Kill a process and wait for the process to actually terminate.
- process.kill
- $1 (Number): PID of the process to kill
- $2 (String)[default: TERM]: Signal to send
- $2 (Number)[default: 3]: Seconds to wait for the process to end: if zero, kill the process and return immediately
- 0 if process is successfully killed, 1 otherwise (not killed or not ended before the timeout period)
Append the provided path to the PATH
environment variable if not yet present.
- env.PATH.append-item
- $1 (String): Path to append
Prepend the provided path to the PATH
environment variable if not yet present.
- env.PATH.prepend-item
- $1 (String): Path to prepend
Create a fifo
in shared memory (/dev/shm
) and return his path
- file.mkfifo_
- The path of the newly create
fifo
Execute a command and return the first line of his standard output (or an empty string if no output is present).
It use a fifo
and appropriate redirection to get the standard output of the command without using a subshell (see this Stackoverflow answer).
- command.stdout_
- ... (String): Command to execute
- The status code of the executed command
- The standard output of the provided command
Execute a command and store the first line of his standard output (or an empty string if no output is present) to the provided variable name.
It use a fifo
and appropriate redirection to get the standard output of the command without using a subshell (see this Stackoverflow answer).
- var.assign
- $1 (String): Variable name where to store the standard output
- ... (String): Command to execute
- The status code of the executed command
Check if the provided variable is defined (regardless if it contains a null string or not)
- var.is-set
- $1 (String): Variable name to check
- Standard (0 for true, 1 for false)
Return true if the provided setting is enabled
- settings.is-enabled
- $1 (String): The setting name to check
- Standard (0 for true, 1 for false)
$ settings.is-enabled TEST
# exitcode=1
$ settings.enable TEST
$ settings.is-enabled TEST && echo ENABLED
ENABLED
Return true if the provided setting is disabled. The function only test if the setting has been explicitly disabled: testing a setting not being defined will return false.
- settings.is-disabled
- $1 (String): The setting name to check
- Standard (0 for true, 1 for false)
$ settings.is-disabled TEST
# exitcode=1
$ settings.enable TEST
$ settings.is-disabled TEST
# exitcode=1
$ settings.disable TEST
$ settings.is-disabled TEST && echo DISABLED
DISABLED
Enable the provided setting.
- settings.enable
- $1 (String): The setting to enable
$ settings.is-enabled TEST
# exitcode=1
$ settings.enable TEST
$ settings.is-enabled TEST && echo ENABLED
ENABLED
Disable the provided setting.
- settings.disable
- $1 (String): The setting to disable
$ settings.is-disabled TEST
# exitcode=1
$ settings.disable TEST
$ settings.is-disabled TEST && echo DISABLED
DISABLED
Set the value of a setting.
- settings.set
- $1 (String): The setting to set
- $2 (String): The value to set
$ settings.set COLOR Red
$ settings.get_ COLOR
# return __="Red"
Get the value of a setting.
- settings.get_
- $1 (String): The setting from which to retrieve the value
$ settings.set COLOR Red
$ settings.get_ COLOR
# return __="Red"