shell

shell

Utility functions for shell command execution.

Example

shell_run(“echo Hello, World!”) ’Hello, World!

’ >>> shell_run(“invalid_command”) ‘/bin/sh: invalid_command: command not found’

Functions

Name Description
concat_newline Concatenates strings with a newline character between non-empty arguments
exec_in_context Executes a function in a context manager and captures the output from stdout and stderr.
shell_run Run a shell command and return stdout/stderr

concat_newline

shell.concat_newline(*args)

Concatenates strings with a newline character between non-empty arguments

Parameters

Name Type Description Default
*args str Variable length argument list of strings to be concatenated. ()

Returns

Name Type Description
string str The concatenated string with newline characters between each non-empty argument.

exec_in_context

shell.exec_in_context(func, *args, **kwargs)

Executes a function in a context manager and captures the output from stdout and stderr.

Parameters

Name Type Description Default
func func The function to be executed. required
*args str Variable length argument list to be passed to the function. ()
**kwargs str Arbitrary keyword arguments to be passed to the function. {}

Returns

Name Type Description
out str The combined output from both stdout and stderr.

shell_run

shell.shell_run(
    command_str,
    capture_output=True,
    check=True,
    shell=True,
    text=True,
)

Run a shell command and return stdout/stderr

Parameters

Name Type Description Default
command_str str The shell command to be executed. required
capture_output bool Whether to capture the command’s output. Defaults to True. True
check bool Whether to raise an exception if the command returns a non-zero exit status. Defaults to True. True
shell bool Whether to run the command through the shell. Defaults to True. True
text bool Whether to treat the command’s input/output as text. Defaults to True. True

Returns

Name Type Description
out str The combined stdout and stderr of the command, separated by a newline character.