Re: [PATCH] tee: Add -q, --quiet, --silent option to not write to stdout

2021-01-23 Thread Otto Moerbeek
On Sat, Jan 23, 2021 at 03:28:01PM +, Stuart Henderson wrote:

> [cc's trimmed]
> 
> On 2021/01/23 15:53, Alejandro Colomar wrote:
> > This is useful for using tee to just write to a file,
> > at the end of a pipeline,
> > without having to redirect to /dev/null
> > 
> > Example:
> > 
> > echo 'foo' | sudo tee -q /etc/foo;
> > 
> > is equivalent to the old (and ugly)
> > 
> > echo 'foo' | sudo tee /etc/foo >/dev/null;
> 
> If this added a new very useful feature then *maybe* it would be
> worthwhile. But as things stand, as an alternative way to do something
> which can already be done trivially, why would you want to encourage
> unportable scripts by adding it?
> 
> It's much less ugly to use >/dev/null than to create a script which
> will fail (possibly in bad ways) on slightly older OS releases.
> 
> I seriously doubt this will be added to OpenBSD.

Agreed. Using a general concept like redirection is much better that
adding specific options to commands. I have no idea why redirection
would be considered ugly.

-Otto



Re: [PATCH] tee: Add -q, --quiet, --silent option to not write to stdout

2021-01-23 Thread Stuart Henderson
[cc's trimmed]

On 2021/01/23 15:53, Alejandro Colomar wrote:
> This is useful for using tee to just write to a file,
> at the end of a pipeline,
> without having to redirect to /dev/null
> 
> Example:
> 
> echo 'foo' | sudo tee -q /etc/foo;
> 
> is equivalent to the old (and ugly)
> 
> echo 'foo' | sudo tee /etc/foo >/dev/null;

If this added a new very useful feature then *maybe* it would be
worthwhile. But as things stand, as an alternative way to do something
which can already be done trivially, why would you want to encourage
unportable scripts by adding it?

It's much less ugly to use >/dev/null than to create a script which
will fail (possibly in bad ways) on slightly older OS releases.

I seriously doubt this will be added to OpenBSD.



[PATCH] tee: Add -q, --quiet, --silent option to not write to stdout

2021-01-23 Thread Alejandro Colomar
This is useful for using tee to just write to a file,
at the end of a pipeline,
without having to redirect to /dev/null

Example:

echo 'foo' | sudo tee -q /etc/foo;

is equivalent to the old (and ugly)

echo 'foo' | sudo tee /etc/foo >/dev/null;
---

v2: Add --silent synonym to --quiet, per GNU guidelines.
I tested --silent with success.

v3: Added -q to opstring, which I removed by accident in v2.
I tested all -q, --quiet and --silent this time.


 src/tee.c | 18 +++---
 1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/src/tee.c b/src/tee.c
index c81faea91..1dfa92cf2 100644
--- a/src/tee.c
+++ b/src/tee.c
@@ -45,6 +45,9 @@ static bool append;
 /* If true, ignore interrupts. */
 static bool ignore_interrupts;
 
+/* Don't write to stdout */
+static bool quiet;
+
 enum output_error
   {
 output_error_sigpipe,  /* traditional behavior, sigpipe enabled.  */
@@ -61,6 +64,8 @@ static struct option const long_options[] =
   {"append", no_argument, NULL, 'a'},
   {"ignore-interrupts", no_argument, NULL, 'i'},
   {"output-error", optional_argument, NULL, 'p'},
+  {"quiet", no_argument, NULL, 'q'},
+  {"silent", no_argument, NULL, 'q'},
   {GETOPT_HELP_OPTION_DECL},
   {GETOPT_VERSION_OPTION_DECL},
   {NULL, 0, NULL, 0}
@@ -93,6 +98,7 @@ Copy standard input to each FILE, and also to standard 
output.\n\
 "), stdout);
   fputs (_("\
   -pdiagnose errors writing to non pipes\n\
+  -q, --quiet, --silent don't write to standard output\n\
   --output-error[=MODE]   set behavior on write error.  See MODE below\n\
 "), stdout);
   fputs (HELP_OPTION_DESCRIPTION, stdout);
@@ -130,8 +136,9 @@ main (int argc, char **argv)
 
   append = false;
   ignore_interrupts = false;
+  quiet = false;
 
-  while ((optc = getopt_long (argc, argv, "aip", long_options, NULL)) != -1)
+  while ((optc = getopt_long (argc, argv, "aipq", long_options, NULL)) != -1)
 {
   switch (optc)
 {
@@ -151,6 +158,10 @@ main (int argc, char **argv)
 output_error = output_error_warn_nopipe;
   break;
 
+case 'q':
+  quiet = true;
+  break;
+
 case_GETOPT_HELP_CHAR;
 
 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
@@ -235,8 +246,9 @@ tee_files (int nfiles, char **files)
 break;
 
   /* Write to all NFILES + 1 descriptors.
- Standard output is the first one.  */
-  for (i = 0; i <= nfiles; i++)
+ Standard output is the first one.
+ If 'quiet' is true, write to descriptors 1 and above (omit stdout)  */
+  for (i = quiet; i <= nfiles; i++)
 if (descriptors[i]
 && fwrite (buffer, bytes_read, 1, descriptors[i]) != 1)
   {
-- 
2.30.0