Skip to content

Commit

Permalink
Merging pull request 130
Browse files Browse the repository at this point in the history
Signed-off-by: Lukáš Doktor <[email protected]>

* github.com:autotest/aexpect:
  Add grep_pipe
  • Loading branch information
ldoktor committed Jun 19, 2024
2 parents 1a51f72 + 1f425a1 commit 4e5ac96
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions aexpect/ops_linux.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,29 @@ def grep(session, expr, path, check=False, flags="-a"):
return output


def grep_pipe(session, cmd, expr, check=False, flags="-a"):
"""
Invoke ``grep`` remotely searching for an expression in a command output.
:param session: session to run the command on
:type session: ShellSession
:param str cmd: command whose output will be grepped
:param str expr: search expression
:param bool check: whether to quietly run grep for a boolean check
:param str flags: extra flags passed to ``grep`` on the command line
:returns: whether there is a match or not or what ``grep`` emits on stdout
if the check mode is disabled
:rtype: bool or str
:raises: ShellCmdError if the check mode is disabled and status is nonzero
"""
grep_command = f"{cmd} | grep {flags} '{expr}'"
status, output = session.cmd_status_output(grep_command)
if check:
return status == 0
_, output = _process_status_output(grep_command, status, output)
return output


###############################################################################
# utilities
###############################################################################
Expand Down

0 comments on commit 4e5ac96

Please sign in to comment.