Read from file descriptor and write to stdout

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












I want to prepend something to each line of output in a script, for every command.



I was thinking of doing something like this:



rm foo
mkfifo foo

exec 3<>foo

cat <&3 | while read line; do
if [[ -n "$line" ]]; then
echo " [prepend] $line";
fi
done &

echo "foo" >&3
echo "bar" >&3
echo "baz" >&3


basically for all commands I want to prepend something to each line of the output. My code above is fairly bogus, but I don't quite know how to do it, it's something like the above but not quite.







share|improve this question

























    up vote
    0
    down vote

    favorite












    I want to prepend something to each line of output in a script, for every command.



    I was thinking of doing something like this:



    rm foo
    mkfifo foo

    exec 3<>foo

    cat <&3 | while read line; do
    if [[ -n "$line" ]]; then
    echo " [prepend] $line";
    fi
    done &

    echo "foo" >&3
    echo "bar" >&3
    echo "baz" >&3


    basically for all commands I want to prepend something to each line of the output. My code above is fairly bogus, but I don't quite know how to do it, it's something like the above but not quite.







    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I want to prepend something to each line of output in a script, for every command.



      I was thinking of doing something like this:



      rm foo
      mkfifo foo

      exec 3<>foo

      cat <&3 | while read line; do
      if [[ -n "$line" ]]; then
      echo " [prepend] $line";
      fi
      done &

      echo "foo" >&3
      echo "bar" >&3
      echo "baz" >&3


      basically for all commands I want to prepend something to each line of the output. My code above is fairly bogus, but I don't quite know how to do it, it's something like the above but not quite.







      share|improve this question













      I want to prepend something to each line of output in a script, for every command.



      I was thinking of doing something like this:



      rm foo
      mkfifo foo

      exec 3<>foo

      cat <&3 | while read line; do
      if [[ -n "$line" ]]; then
      echo " [prepend] $line";
      fi
      done &

      echo "foo" >&3
      echo "bar" >&3
      echo "baz" >&3


      basically for all commands I want to prepend something to each line of the output. My code above is fairly bogus, but I don't quite know how to do it, it's something like the above but not quite.









      share|improve this question












      share|improve this question




      share|improve this question








      edited May 8 at 4:17
























      asked May 8 at 4:01









      Alexander Mills

      1,885929




      1,885929




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          1
          down vote













          Assuming script produces:



          L1
          L2

          L4
          L5


          then the following command



          script | sed 's/^(.+)/ [prepend] 1/'


          prepends " [prepend] " to each non-empty line:



           [prepend] L1
          [prepend] L2

          [prepend] L4
          [prepend] L5





          share|improve this answer




























            up vote
            1
            down vote













            You may want to look at the DEBUG trap in bash. From man builtins:



            If a sigspec is DEBUG, the command arg is executed before every simple command,
            for command, case command, select command, every arithmetic for command, and
            before the first command executes in a shell function (see SHELL GRAMMAR
            above). Refer to the description of the extdebug option to the shopt builtin
            for details of its effect on the DEBUG trap. If a sigspec is RETURN, the com‐
            mand arg is executed each time a shell function or a script executed with the .
            or source builtins finishes executing.


            So, you could set up a debug function like this. Since it runs before commands you could use it to prepend to your output.



            #!/bin/bash

            debug()
            : # echo or other commands here


            trap debug DEBUG

            # Commands here





            share|improve this answer




























              up vote
              0
              down vote













              This exact code seems to do what I want, but not sure how safe it is:



              rm foo
              mkfifo foo

              exec 3<>foo

              (
              cat <&3 | while read line; do
              if [[ -n "$line" ]]; then
              echo " [prepend] $line";
              fi
              done &
              )

              echo "" >&3;
              echo "" >&3;
              echo "foo" >&3
              echo "bar" >&3
              echo "baz" >&3


              pkill -P $$





              share|improve this answer





















                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',
                convertImagesToLinks: false,
                noModals: false,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: null,
                bindNavPrevention: true,
                postfix: "",
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                );



                );








                 

                draft saved


                draft discarded


















                StackExchange.ready(
                function ()
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f442455%2fread-from-file-descriptor-and-write-to-stdout%23new-answer', 'question_page');

                );

                Post as a guest






























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes








                up vote
                1
                down vote













                Assuming script produces:



                L1
                L2

                L4
                L5


                then the following command



                script | sed 's/^(.+)/ [prepend] 1/'


                prepends " [prepend] " to each non-empty line:



                 [prepend] L1
                [prepend] L2

                [prepend] L4
                [prepend] L5





                share|improve this answer

























                  up vote
                  1
                  down vote













                  Assuming script produces:



                  L1
                  L2

                  L4
                  L5


                  then the following command



                  script | sed 's/^(.+)/ [prepend] 1/'


                  prepends " [prepend] " to each non-empty line:



                   [prepend] L1
                  [prepend] L2

                  [prepend] L4
                  [prepend] L5





                  share|improve this answer























                    up vote
                    1
                    down vote










                    up vote
                    1
                    down vote









                    Assuming script produces:



                    L1
                    L2

                    L4
                    L5


                    then the following command



                    script | sed 's/^(.+)/ [prepend] 1/'


                    prepends " [prepend] " to each non-empty line:



                     [prepend] L1
                    [prepend] L2

                    [prepend] L4
                    [prepend] L5





                    share|improve this answer













                    Assuming script produces:



                    L1
                    L2

                    L4
                    L5


                    then the following command



                    script | sed 's/^(.+)/ [prepend] 1/'


                    prepends " [prepend] " to each non-empty line:



                     [prepend] L1
                    [prepend] L2

                    [prepend] L4
                    [prepend] L5






                    share|improve this answer













                    share|improve this answer



                    share|improve this answer











                    answered May 8 at 22:11









                    John Doe

                    804




                    804






















                        up vote
                        1
                        down vote













                        You may want to look at the DEBUG trap in bash. From man builtins:



                        If a sigspec is DEBUG, the command arg is executed before every simple command,
                        for command, case command, select command, every arithmetic for command, and
                        before the first command executes in a shell function (see SHELL GRAMMAR
                        above). Refer to the description of the extdebug option to the shopt builtin
                        for details of its effect on the DEBUG trap. If a sigspec is RETURN, the com‐
                        mand arg is executed each time a shell function or a script executed with the .
                        or source builtins finishes executing.


                        So, you could set up a debug function like this. Since it runs before commands you could use it to prepend to your output.



                        #!/bin/bash

                        debug()
                        : # echo or other commands here


                        trap debug DEBUG

                        # Commands here





                        share|improve this answer

























                          up vote
                          1
                          down vote













                          You may want to look at the DEBUG trap in bash. From man builtins:



                          If a sigspec is DEBUG, the command arg is executed before every simple command,
                          for command, case command, select command, every arithmetic for command, and
                          before the first command executes in a shell function (see SHELL GRAMMAR
                          above). Refer to the description of the extdebug option to the shopt builtin
                          for details of its effect on the DEBUG trap. If a sigspec is RETURN, the com‐
                          mand arg is executed each time a shell function or a script executed with the .
                          or source builtins finishes executing.


                          So, you could set up a debug function like this. Since it runs before commands you could use it to prepend to your output.



                          #!/bin/bash

                          debug()
                          : # echo or other commands here


                          trap debug DEBUG

                          # Commands here





                          share|improve this answer























                            up vote
                            1
                            down vote










                            up vote
                            1
                            down vote









                            You may want to look at the DEBUG trap in bash. From man builtins:



                            If a sigspec is DEBUG, the command arg is executed before every simple command,
                            for command, case command, select command, every arithmetic for command, and
                            before the first command executes in a shell function (see SHELL GRAMMAR
                            above). Refer to the description of the extdebug option to the shopt builtin
                            for details of its effect on the DEBUG trap. If a sigspec is RETURN, the com‐
                            mand arg is executed each time a shell function or a script executed with the .
                            or source builtins finishes executing.


                            So, you could set up a debug function like this. Since it runs before commands you could use it to prepend to your output.



                            #!/bin/bash

                            debug()
                            : # echo or other commands here


                            trap debug DEBUG

                            # Commands here





                            share|improve this answer













                            You may want to look at the DEBUG trap in bash. From man builtins:



                            If a sigspec is DEBUG, the command arg is executed before every simple command,
                            for command, case command, select command, every arithmetic for command, and
                            before the first command executes in a shell function (see SHELL GRAMMAR
                            above). Refer to the description of the extdebug option to the shopt builtin
                            for details of its effect on the DEBUG trap. If a sigspec is RETURN, the com‐
                            mand arg is executed each time a shell function or a script executed with the .
                            or source builtins finishes executing.


                            So, you could set up a debug function like this. Since it runs before commands you could use it to prepend to your output.



                            #!/bin/bash

                            debug()
                            : # echo or other commands here


                            trap debug DEBUG

                            # Commands here






                            share|improve this answer













                            share|improve this answer



                            share|improve this answer











                            answered May 8 at 23:20









                            m0dular

                            63115




                            63115




















                                up vote
                                0
                                down vote













                                This exact code seems to do what I want, but not sure how safe it is:



                                rm foo
                                mkfifo foo

                                exec 3<>foo

                                (
                                cat <&3 | while read line; do
                                if [[ -n "$line" ]]; then
                                echo " [prepend] $line";
                                fi
                                done &
                                )

                                echo "" >&3;
                                echo "" >&3;
                                echo "foo" >&3
                                echo "bar" >&3
                                echo "baz" >&3


                                pkill -P $$





                                share|improve this answer

























                                  up vote
                                  0
                                  down vote













                                  This exact code seems to do what I want, but not sure how safe it is:



                                  rm foo
                                  mkfifo foo

                                  exec 3<>foo

                                  (
                                  cat <&3 | while read line; do
                                  if [[ -n "$line" ]]; then
                                  echo " [prepend] $line";
                                  fi
                                  done &
                                  )

                                  echo "" >&3;
                                  echo "" >&3;
                                  echo "foo" >&3
                                  echo "bar" >&3
                                  echo "baz" >&3


                                  pkill -P $$





                                  share|improve this answer























                                    up vote
                                    0
                                    down vote










                                    up vote
                                    0
                                    down vote









                                    This exact code seems to do what I want, but not sure how safe it is:



                                    rm foo
                                    mkfifo foo

                                    exec 3<>foo

                                    (
                                    cat <&3 | while read line; do
                                    if [[ -n "$line" ]]; then
                                    echo " [prepend] $line";
                                    fi
                                    done &
                                    )

                                    echo "" >&3;
                                    echo "" >&3;
                                    echo "foo" >&3
                                    echo "bar" >&3
                                    echo "baz" >&3


                                    pkill -P $$





                                    share|improve this answer













                                    This exact code seems to do what I want, but not sure how safe it is:



                                    rm foo
                                    mkfifo foo

                                    exec 3<>foo

                                    (
                                    cat <&3 | while read line; do
                                    if [[ -n "$line" ]]; then
                                    echo " [prepend] $line";
                                    fi
                                    done &
                                    )

                                    echo "" >&3;
                                    echo "" >&3;
                                    echo "foo" >&3
                                    echo "bar" >&3
                                    echo "baz" >&3


                                    pkill -P $$






                                    share|improve this answer













                                    share|improve this answer



                                    share|improve this answer











                                    answered May 8 at 4:25









                                    Alexander Mills

                                    1,885929




                                    1,885929






















                                         

                                        draft saved


                                        draft discarded


























                                         


                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function ()
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f442455%2fread-from-file-descriptor-and-write-to-stdout%23new-answer', 'question_page');

                                        );

                                        Post as a guest













































































                                        Popular posts from this blog

                                        How to check contact read email or not when send email to Individual?

                                        Bahrain

                                        Postfix configuration issue with fips on centos 7; mailgun relay