Re: grep: add option for better usability in pipelines

2022-02-16 Thread Kang-Che Sung
On Thu, Feb 17, 2022 at 3:27 PM Ulrich Eckhardt
 wrote:
> >
> > Why do we need to implement a workaround in grep while you can
> > do this in shell to ignore the exit code of grep?
> >
> > { grep issue 
> `grep -p ...` rather replaces `grep ... || test $? = 1`. The advantage
> is that it is explicit about what it does. Understanding the intent of
> `-p` or `--pipe` is easier than understanding the alternative.
>

Unless you can propose the '-p' option to POSIX will busybox implement it as
you say.

The problem here is portability. FreeBSD grep
(https://www.freebsd.org/cgi/man.cgi?query=grep) has a different meaning with
'-p' so it won't be portable to use '-p' in shell scripts. And the long option
'--pipe' isn't quite helpful either as it would end up being a GNU-only
extension and so is discouraged to use in shell scripts (which defeats the
purpose of your '--pipe' option in the first place).
___
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox


Re: grep: add option for better usability in pipelines

2022-02-16 Thread Kang-Che Sung
On Thu, Feb 17, 2022 at 12:50 PM Michael Conrad  wrote:
>
> On 2/16/22 19:32, Kang-Che Sung wrote:
> >
> >> Now, for an example where it makes a difference. Consider a Bash script
> >> like this:
> >>
> >># enable automatic error handling
> >>set -eo pipefail
> >># check for string "issues" in a logfile
> >>cat logfile | grep issue | sort --unique
> >>
> >> If there are no issues in the logs, grep return exit code 1 and the
> >> shell interprets this as an error and exits itself.
> >>
> > Why do we need to implement a workaround in grep while you can
> > do this in shell to ignore the exit code of grep?
> >
> > { grep issue 
> In order to implement his suggestion, you need to only ignore exit code
> 1, while still allowing other exit codes to abort the script. (like a
> system error while reading the file)

{ grep issue  But also, I've never seen ":" used as a command... where is that
> documented?  Is it equivalent to 'true'?

https://www.gnu.org/savannah-checkouts/gnu/autoconf/manual/autoconf-2.71/html_node/Limitations-of-Builtins.html
Look for the "true" section.
___
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox


Re: grep: add option for better usability in pipelines

2022-02-16 Thread Ulrich Eckhardt
On Thu, 17 Feb 2022 08:32:06 +0800
Kang-Che Sung  wrote:
> On Thu, Feb 17, 2022 at 8:01 AM Ulrich Eckhardt
>  wrote:
> > Now, for an example where it makes a difference. Consider a Bash
> > script like this:
> >
> >   # enable automatic error handling
> >   set -eo pipefail
> >   # check for string "issues" in a logfile
> >   cat logfile | grep issue | sort --unique
> >
> > If there are no issues in the logs, grep return exit code 1 and the
> > shell interprets this as an error and exits itself.
> >
> 
> Why do we need to implement a workaround in grep while you can
> do this in shell to ignore the exit code of grep?
> 
> { grep issue  > I've implemented that feature here, though it lacks tests yet:
> > https://github.com/UlrichEckhardt/busybox/tree/grep-pipe-option
> > Also, I'm currently trying to get the same feature into GNU grep as
> > well, the long form `--pipe` is used there. I've also considered
> > `--filter` (because it only filters) as alternative. I'm not fully
> > happy with either one, maybe someone here comes up with a better
> > suggestion.
> 
> I don't see the '--pipe' option in GNU grep manual
> 

Misunderstanding: It's not in there! I have it implemented already and
I'm trying to get it incorporated there, which takes time due to FSF
paperwork.


Cheers!


Uli
___
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox


Re: grep: add option for better usability in pipelines

2022-02-16 Thread Michael Conrad

On 2/16/22 19:32, Kang-Che Sung wrote:



Now, for an example where it makes a difference. Consider a Bash script
like this:

   # enable automatic error handling
   set -eo pipefail
   # check for string "issues" in a logfile
   cat logfile | grep issue | sort --unique

If there are no issues in the logs, grep return exit code 1 and the
shell interprets this as an error and exits itself.


Why do we need to implement a workaround in grep while you can
do this in shell to ignore the exit code of grep?

{ grep issue 

In order to implement his suggestion, you need to only ignore exit code 
1, while still allowing other exit codes to abort the script. (like a 
system error while reading the file)


But also, I've never seen ":" used as a command... where is that 
documented?  Is it equivalent to 'true'?


-Mike C
___
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox


Re: grep: add option for better usability in pipelines

2022-02-16 Thread Kang-Che Sung
On Thu, Feb 17, 2022 at 8:01 AM Ulrich Eckhardt
 wrote:
>
> I've implemented a `-p` flag for grep, which disables a behaviour that
> the exit status is used (abused?) to signal whether any match
> occurred.

The behaviour is POSIX.

> Now, for an example where it makes a difference. Consider a Bash script
> like this:
>
>   # enable automatic error handling
>   set -eo pipefail
>   # check for string "issues" in a logfile
>   cat logfile | grep issue | sort --unique
>
> If there are no issues in the logs, grep return exit code 1 and the
> shell interprets this as an error and exits itself.
>

Why do we need to implement a workaround in grep while you can
do this in shell to ignore the exit code of grep?

{ grep issue  I've implemented that feature here, though it lacks tests yet:
> https://github.com/UlrichEckhardt/busybox/tree/grep-pipe-option
> Also, I'm currently trying to get the same feature into GNU grep as
> well, the long form `--pipe` is used there. I've also considered
> `--filter` (because it only filters) as alternative. I'm not fully
> happy with either one, maybe someone here comes up with a better
> suggestion.

I don't see the '--pipe' option in GNU grep manual

and I don't think it would be needed considering that you can tell the
shell to ignore an exit code already.
___
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox


grep: add option for better usability in pipelines

2022-02-16 Thread Ulrich Eckhardt
Greetings!

I've implemented a `-p` flag for grep, which disables a behaviour that
the exit status is used (abused?) to signal whether any match
occurred. In many cases, you won't notice this. Also, in many cases,
shell scripts don't check for errors.

Now, for an example where it makes a difference. Consider a Bash script
like this:

  # enable automatic error handling
  set -eo pipefail
  # check for string "issues" in a logfile
  cat logfile | grep issue | sort --unique

If there are no issues in the logs, grep return exit code 1 and the
shell interprets this as an error and exits itself.

I've implemented that feature here, though it lacks tests yet:
https://github.com/UlrichEckhardt/busybox/tree/grep-pipe-option
Also, I'm currently trying to get the same feature into GNU grep as
well, the long form `--pipe` is used there. I've also considered
`--filter` (because it only filters) as alternative. I'm not fully
happy with either one, maybe someone here comes up with a better
suggestion.

If you want, grab the first commit in there, it just fixes a small typo.

I'd be happy to hear any suggestions!


Cheers!


Uli
___
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox