Append string on redirect












0















When redirecting all output to stderr



stat file >&2


If the command throws an error (e.g. file does not exist) how can one prepend some arbitraty text e.g.



<<MY ARBITRARY TEXT>>: stat: cannot stat 'file': No such file or directory


The required logic is:



let res = stat file
if error then output "<<MY ARBITRARY TEXT>>"+error to stderr
else output res to stderr


More context: all command output is being redirected to stderr because stdout and stderr streams do always return in the right order, i.e. if multiple commands are sent and one throw an error, the stdout of the next command may return before the stderr of the original command. To avoid this I am merging all commands onto stderr, but ideally would like to be able to easily identify errors.










share|improve this question







New contributor




TrevTheDev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    0















    When redirecting all output to stderr



    stat file >&2


    If the command throws an error (e.g. file does not exist) how can one prepend some arbitraty text e.g.



    <<MY ARBITRARY TEXT>>: stat: cannot stat 'file': No such file or directory


    The required logic is:



    let res = stat file
    if error then output "<<MY ARBITRARY TEXT>>"+error to stderr
    else output res to stderr


    More context: all command output is being redirected to stderr because stdout and stderr streams do always return in the right order, i.e. if multiple commands are sent and one throw an error, the stdout of the next command may return before the stderr of the original command. To avoid this I am merging all commands onto stderr, but ideally would like to be able to easily identify errors.










    share|improve this question







    New contributor




    TrevTheDev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.























      0












      0








      0








      When redirecting all output to stderr



      stat file >&2


      If the command throws an error (e.g. file does not exist) how can one prepend some arbitraty text e.g.



      <<MY ARBITRARY TEXT>>: stat: cannot stat 'file': No such file or directory


      The required logic is:



      let res = stat file
      if error then output "<<MY ARBITRARY TEXT>>"+error to stderr
      else output res to stderr


      More context: all command output is being redirected to stderr because stdout and stderr streams do always return in the right order, i.e. if multiple commands are sent and one throw an error, the stdout of the next command may return before the stderr of the original command. To avoid this I am merging all commands onto stderr, but ideally would like to be able to easily identify errors.










      share|improve this question







      New contributor




      TrevTheDev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.












      When redirecting all output to stderr



      stat file >&2


      If the command throws an error (e.g. file does not exist) how can one prepend some arbitraty text e.g.



      <<MY ARBITRARY TEXT>>: stat: cannot stat 'file': No such file or directory


      The required logic is:



      let res = stat file
      if error then output "<<MY ARBITRARY TEXT>>"+error to stderr
      else output res to stderr


      More context: all command output is being redirected to stderr because stdout and stderr streams do always return in the right order, i.e. if multiple commands are sent and one throw an error, the stdout of the next command may return before the stderr of the original command. To avoid this I am merging all commands onto stderr, but ideally would like to be able to easily identify errors.







      bash shell-script shell






      share|improve this question







      New contributor




      TrevTheDev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      TrevTheDev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      TrevTheDev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 40 mins ago









      TrevTheDevTrevTheDev

      1134




      1134




      New contributor




      TrevTheDev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      TrevTheDev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      TrevTheDev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          1 Answer
          1






          active

          oldest

          votes


















          0














          TL;DR: Parsing stderr on the fly can be complicated, so simplify it by storing stderr into variable and operate variable contents later.



          What you may want to consider doing is capturing stderr to variable, and afterwards checking if it's not empty. Consider the example below:



          $ foo=$(stat /etc/passwd 2>&1  > /dev/tty )
          File: /etc/passwd
          Size: 1710 Blocks: 8 IO Block: 4096 regular file
          Device: fd00h/64768d Inode: 537594 Links: 1
          Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
          Access: 2019-03-08 15:28:10.481688274 -0700
          Modify: 2019-03-08 15:28:10.333669255 -0700
          Change: 2019-03-08 15:28:10.341670283 -0700
          Birth: -
          $ test "x$foo" = "x" && echo "empty"
          empty


          Command substitution $(...) runs a subshell and captures stdout only, hence we want to duplicate the file descriptor 1 onto 2 2>&1 so that stderr now can be captured by command substitution, however we still want to see normal output on screen, hence afterwards we point stdout at the controlling terminal /dev/tty.



          Now, what if we actually capture stderr into the variable ?



          $ foo=$(stat nonexistent.txt 2>&1  > /dev/tty )
          $ test "x$foo" = "x" && echo "empty" || echo "not empty"
          not empty
          $ printf "<Arbitrary Text>%sn" "$foo"
          <Arbitrary Text>stat: cannot stat 'nonexistent.txt': No such file or directory


          As you can see the because stderr is captured we can wrap arbitrary text around it as we wish. The main reason for using a variable is that standard shell methods such as pipeline and command substitution operate on stdout file descriptor 1, so there's not a lot we can do to parse stderr on the fly. Of course, we could use a named pipe and redirect to it as stat foobar.txt 2> /tmp/named_pipe.fifo, but the problem with that is blocking - information sent to pipes is buffered until consumed by another process, so your shell script will be stuck. Of course, this could be handled via starting a background process, but IMHO it's more complexity than necessary, and doing multiple processes is far better done in a programming language like Python where you can actually directly access multiprocessing calls.



          Side note: test and [ are the same command, so you could just as well write [ "x$foo" = "x" ] and use that in a more elaborate if-else statement





          share

























            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "106"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });






            TrevTheDev is a new contributor. Be nice, and check out our Code of Conduct.










            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f505260%2fappend-string-on-redirect%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            TL;DR: Parsing stderr on the fly can be complicated, so simplify it by storing stderr into variable and operate variable contents later.



            What you may want to consider doing is capturing stderr to variable, and afterwards checking if it's not empty. Consider the example below:



            $ foo=$(stat /etc/passwd 2>&1  > /dev/tty )
            File: /etc/passwd
            Size: 1710 Blocks: 8 IO Block: 4096 regular file
            Device: fd00h/64768d Inode: 537594 Links: 1
            Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
            Access: 2019-03-08 15:28:10.481688274 -0700
            Modify: 2019-03-08 15:28:10.333669255 -0700
            Change: 2019-03-08 15:28:10.341670283 -0700
            Birth: -
            $ test "x$foo" = "x" && echo "empty"
            empty


            Command substitution $(...) runs a subshell and captures stdout only, hence we want to duplicate the file descriptor 1 onto 2 2>&1 so that stderr now can be captured by command substitution, however we still want to see normal output on screen, hence afterwards we point stdout at the controlling terminal /dev/tty.



            Now, what if we actually capture stderr into the variable ?



            $ foo=$(stat nonexistent.txt 2>&1  > /dev/tty )
            $ test "x$foo" = "x" && echo "empty" || echo "not empty"
            not empty
            $ printf "<Arbitrary Text>%sn" "$foo"
            <Arbitrary Text>stat: cannot stat 'nonexistent.txt': No such file or directory


            As you can see the because stderr is captured we can wrap arbitrary text around it as we wish. The main reason for using a variable is that standard shell methods such as pipeline and command substitution operate on stdout file descriptor 1, so there's not a lot we can do to parse stderr on the fly. Of course, we could use a named pipe and redirect to it as stat foobar.txt 2> /tmp/named_pipe.fifo, but the problem with that is blocking - information sent to pipes is buffered until consumed by another process, so your shell script will be stuck. Of course, this could be handled via starting a background process, but IMHO it's more complexity than necessary, and doing multiple processes is far better done in a programming language like Python where you can actually directly access multiprocessing calls.



            Side note: test and [ are the same command, so you could just as well write [ "x$foo" = "x" ] and use that in a more elaborate if-else statement





            share






























              0














              TL;DR: Parsing stderr on the fly can be complicated, so simplify it by storing stderr into variable and operate variable contents later.



              What you may want to consider doing is capturing stderr to variable, and afterwards checking if it's not empty. Consider the example below:



              $ foo=$(stat /etc/passwd 2>&1  > /dev/tty )
              File: /etc/passwd
              Size: 1710 Blocks: 8 IO Block: 4096 regular file
              Device: fd00h/64768d Inode: 537594 Links: 1
              Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
              Access: 2019-03-08 15:28:10.481688274 -0700
              Modify: 2019-03-08 15:28:10.333669255 -0700
              Change: 2019-03-08 15:28:10.341670283 -0700
              Birth: -
              $ test "x$foo" = "x" && echo "empty"
              empty


              Command substitution $(...) runs a subshell and captures stdout only, hence we want to duplicate the file descriptor 1 onto 2 2>&1 so that stderr now can be captured by command substitution, however we still want to see normal output on screen, hence afterwards we point stdout at the controlling terminal /dev/tty.



              Now, what if we actually capture stderr into the variable ?



              $ foo=$(stat nonexistent.txt 2>&1  > /dev/tty )
              $ test "x$foo" = "x" && echo "empty" || echo "not empty"
              not empty
              $ printf "<Arbitrary Text>%sn" "$foo"
              <Arbitrary Text>stat: cannot stat 'nonexistent.txt': No such file or directory


              As you can see the because stderr is captured we can wrap arbitrary text around it as we wish. The main reason for using a variable is that standard shell methods such as pipeline and command substitution operate on stdout file descriptor 1, so there's not a lot we can do to parse stderr on the fly. Of course, we could use a named pipe and redirect to it as stat foobar.txt 2> /tmp/named_pipe.fifo, but the problem with that is blocking - information sent to pipes is buffered until consumed by another process, so your shell script will be stuck. Of course, this could be handled via starting a background process, but IMHO it's more complexity than necessary, and doing multiple processes is far better done in a programming language like Python where you can actually directly access multiprocessing calls.



              Side note: test and [ are the same command, so you could just as well write [ "x$foo" = "x" ] and use that in a more elaborate if-else statement





              share




























                0












                0








                0







                TL;DR: Parsing stderr on the fly can be complicated, so simplify it by storing stderr into variable and operate variable contents later.



                What you may want to consider doing is capturing stderr to variable, and afterwards checking if it's not empty. Consider the example below:



                $ foo=$(stat /etc/passwd 2>&1  > /dev/tty )
                File: /etc/passwd
                Size: 1710 Blocks: 8 IO Block: 4096 regular file
                Device: fd00h/64768d Inode: 537594 Links: 1
                Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
                Access: 2019-03-08 15:28:10.481688274 -0700
                Modify: 2019-03-08 15:28:10.333669255 -0700
                Change: 2019-03-08 15:28:10.341670283 -0700
                Birth: -
                $ test "x$foo" = "x" && echo "empty"
                empty


                Command substitution $(...) runs a subshell and captures stdout only, hence we want to duplicate the file descriptor 1 onto 2 2>&1 so that stderr now can be captured by command substitution, however we still want to see normal output on screen, hence afterwards we point stdout at the controlling terminal /dev/tty.



                Now, what if we actually capture stderr into the variable ?



                $ foo=$(stat nonexistent.txt 2>&1  > /dev/tty )
                $ test "x$foo" = "x" && echo "empty" || echo "not empty"
                not empty
                $ printf "<Arbitrary Text>%sn" "$foo"
                <Arbitrary Text>stat: cannot stat 'nonexistent.txt': No such file or directory


                As you can see the because stderr is captured we can wrap arbitrary text around it as we wish. The main reason for using a variable is that standard shell methods such as pipeline and command substitution operate on stdout file descriptor 1, so there's not a lot we can do to parse stderr on the fly. Of course, we could use a named pipe and redirect to it as stat foobar.txt 2> /tmp/named_pipe.fifo, but the problem with that is blocking - information sent to pipes is buffered until consumed by another process, so your shell script will be stuck. Of course, this could be handled via starting a background process, but IMHO it's more complexity than necessary, and doing multiple processes is far better done in a programming language like Python where you can actually directly access multiprocessing calls.



                Side note: test and [ are the same command, so you could just as well write [ "x$foo" = "x" ] and use that in a more elaborate if-else statement





                share















                TL;DR: Parsing stderr on the fly can be complicated, so simplify it by storing stderr into variable and operate variable contents later.



                What you may want to consider doing is capturing stderr to variable, and afterwards checking if it's not empty. Consider the example below:



                $ foo=$(stat /etc/passwd 2>&1  > /dev/tty )
                File: /etc/passwd
                Size: 1710 Blocks: 8 IO Block: 4096 regular file
                Device: fd00h/64768d Inode: 537594 Links: 1
                Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
                Access: 2019-03-08 15:28:10.481688274 -0700
                Modify: 2019-03-08 15:28:10.333669255 -0700
                Change: 2019-03-08 15:28:10.341670283 -0700
                Birth: -
                $ test "x$foo" = "x" && echo "empty"
                empty


                Command substitution $(...) runs a subshell and captures stdout only, hence we want to duplicate the file descriptor 1 onto 2 2>&1 so that stderr now can be captured by command substitution, however we still want to see normal output on screen, hence afterwards we point stdout at the controlling terminal /dev/tty.



                Now, what if we actually capture stderr into the variable ?



                $ foo=$(stat nonexistent.txt 2>&1  > /dev/tty )
                $ test "x$foo" = "x" && echo "empty" || echo "not empty"
                not empty
                $ printf "<Arbitrary Text>%sn" "$foo"
                <Arbitrary Text>stat: cannot stat 'nonexistent.txt': No such file or directory


                As you can see the because stderr is captured we can wrap arbitrary text around it as we wish. The main reason for using a variable is that standard shell methods such as pipeline and command substitution operate on stdout file descriptor 1, so there's not a lot we can do to parse stderr on the fly. Of course, we could use a named pipe and redirect to it as stat foobar.txt 2> /tmp/named_pipe.fifo, but the problem with that is blocking - information sent to pipes is buffered until consumed by another process, so your shell script will be stuck. Of course, this could be handled via starting a background process, but IMHO it's more complexity than necessary, and doing multiple processes is far better done in a programming language like Python where you can actually directly access multiprocessing calls.



                Side note: test and [ are the same command, so you could just as well write [ "x$foo" = "x" ] and use that in a more elaborate if-else statement






                share













                share


                share








                edited 3 mins ago

























                answered 9 mins ago









                Sergiy KolodyazhnyySergiy Kolodyazhnyy

                10.5k42663




                10.5k42663






















                    TrevTheDev is a new contributor. Be nice, and check out our Code of Conduct.










                    draft saved

                    draft discarded


















                    TrevTheDev is a new contributor. Be nice, and check out our Code of Conduct.













                    TrevTheDev is a new contributor. Be nice, and check out our Code of Conduct.












                    TrevTheDev is a new contributor. Be nice, and check out our Code of Conduct.
















                    Thanks for contributing an answer to Unix & Linux Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f505260%2fappend-string-on-redirect%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    CARDNET

                    Boot-repair Failure: Unable to locate package grub-common:i386

                    濃尾地震