coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] tee: add a -q/--quiet option


From: Alex Henrie
Subject: [PATCH] tee: add a -q/--quiet option
Date: Wed, 23 Dec 2020 23:01:31 -0700

---
It is very useful to be able to read a file as root and then process it
as an ordinary user, for example:

sudo cat protected.txt | grep foo

However, there is currently no exact equivalent for the opposite:
Generating text as a normal user and writing it to a file as root. `tee`
is commonly used for this purpose:

echo foo | sudo tee protected.txt

The downside is that tee always prints to standard output in addition to
writing to the file. This can be avoided with piping, but it makes the
command quite cumbersome:

echo foo | sudo tee protected.txt > /dev/null

I propose adding a -q/--quiet flag to `tee` which causes it to only
write to the specified files and not to stdout. This would allow the
following convenient command to be used for writing to a protected file:

echo foo | sudo tee -q protected.txt

This would be a GNU extension. Happy holidays!

-Alex
---
 src/tee.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/src/tee.c b/src/tee.c
index decb9832d..c9b337d94 100644
--- a/src/tee.c
+++ b/src/tee.c
@@ -45,6 +45,9 @@ static bool append;
 /* If true, ignore interrupts. */
 static bool ignore_interrupts;
 
+/* If true, don't write to stdout. */
+static bool quiet;
+
 enum output_error
   {
     output_error_sigpipe,      /* traditional behavior, sigpipe enabled.  */
@@ -61,6 +64,7 @@ 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'},
   {GETOPT_HELP_OPTION_DECL},
   {GETOPT_VERSION_OPTION_DECL},
   {NULL, 0, NULL, 0}
@@ -94,6 +98,7 @@ Copy standard input to each FILE, and also to standard 
output.\n\
       fputs (_("\
   -p                        diagnose errors writing to non pipes\n\
       --output-error[=MODE]   set behavior on write error.  See MODE below\n\
+  -q, --quiet               don't write to standard output\n\
 "), stdout);
       fputs (HELP_OPTION_DESCRIPTION, stdout);
       fputs (VERSION_OPTION_DESCRIPTION, stdout);
@@ -130,8 +135,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 +157,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);
@@ -236,7 +246,7 @@ tee_files (int nfiles, char **files)
 
       /* Write to all NFILES + 1 descriptors.
          Standard output is the first one.  */
-      for (i = 0; i <= nfiles; i++)
+      for (i = quiet ? 1 : 0; i <= nfiles; i++)
         if (descriptors[i]
             && fwrite (buffer, bytes_read, 1, descriptors[i]) != 1)
           {
-- 
2.29.2.368.ge46b91665e.dirty




reply via email to

[Prev in Thread] Current Thread [Next in Thread]