shell
shell
Utility functions for shell command execution.
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
*args) shell.concat_newline(
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
*args, **kwargs) shell.exec_in_context(func,
Executes a function in a context manager and captures the output from stdout and stderr.
Args:
func (func): The function to be executed.
*args: Variable length argument list to be passed to the function.
**kwargs: Arbitrary keyword arguments to be passed to the function.
Returns:
out (str): The combined output from both stdout and stderr.
Examples:
>>> exec_in_context(print, "Hello, World!")
'Hello, World!
’
shell_run
shell.shell_run(
command_str,=True,
capture_output=True,
check=True,
shell=True,
text )
Run a shell command and return stdout/stderr
Args:
command_str (str): The shell command to be executed.
capture_output (bool, optional): Whether to capture the command's output. Defaults to True.
check (bool, optional): Whether to raise an exception if the command returns a non-zero exit status. Defaults to True.
shell (bool, optional): Whether to run the command through the shell. Defaults to True.
text (bool, optional): Whether to treat the command's input/output as text. Defaults to True.
Returns:
out (str): The combined stdout and stderr of the command, separated by a newline character.
Examples:
>>> shell_run("echo Hello, World!")
'Hello, World!
’ >>> shell_run(“invalid_command”) ‘/bin/sh: invalid_command: command not found’