Skip to content

Latest commit

 

History

History
57 lines (47 loc) · 2.89 KB

snippet_functions.md

File metadata and controls

57 lines (47 loc) · 2.89 KB

Documentation for the functions in (snippet_functions.sh).

Quick access

Function documentation

If the pipes are not documented, the default is:

  • stdin: piped input ignored
  • stdout: empty

Parameters enclosed in brackets [ ] are optional.

is_command_defined()

Returns with status 0 if the shell has a "definition" for the command $1, regardless what type it is (function, built-in, file, etc.). It's similar to which but may also be used for bash functions and helps avoid "command ... unknown" errors. It may be used in instruction chains:

    is_command_defined "tail" && tail "..."

will only call tail if it's defined (aka installed). The example shows an essential difference with is_function_defined() which will always return status 1 (not defined) for tail because it has the type file, not function.

Param.$1name of the function
Status 0function $1 is defined
1function $1 is not defined

is_function_defined()

Returns with status 0 if a bash function with the name $1 exists. Useful to avoid "command ... unknown" errors.

Important: do not use this function to check for the availability of standard shell utilities like echo or tail f.ex. - these are not considered functions by bash (types builtin and file, respectively). Even if they are defined, this function returns status 1 (not defined) - use is_command_defined() instead.

Example of the use in an instruction chain:

    is_function_defined "log" && log "..."

will only call log if it's defined.

Param.$1name of the function
Status 0function $1 is defined
1function $1 is not defined

get_piped_input()

Returns the piped stdin content on stdout, which allows to capture it into a variable, here f.ex. to $input:

    input="$(get_piped_input)"
Pipes stdinread completely
stdoutcopy of stdin's piped input
Status0