Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add grep_pipe #130

Merged
merged 1 commit into from
Jun 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, it'd be nice to unify this with the existing grep by changing the position of check and flags.

Something like grep_pipe(session, cmd, expr, check, flags) would be IMO better (or even session, expr, cmd, check, flags but logically the cmd should be first here. Not a strong preference over those two, though.)

: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
Loading