shell wrapper to restart script based on its output












1














I've had a bit of shell scripting practice reading piped input from other programs, but am unsure how to approach this problem.



THE BACKSTORY



A program robinbotter whose internals I can't really fix/modify takes its input from files equities.sym and blacklist.sym, each simple text files containing one ticker symbol per line.



When it runs okay, its output produces:



...
Downloading instruments: [ AFL KELYB LFUS ]
...


When it breaks due to internal bugs,



...
Downloading instruments: [ AFL KELYB LFUS LNVGY


and halts there, with no further output, yielding exit code 0 like in the okay case (unfortunately).



The ticker symbols are printed out with slight delay--no newlines in between--while the program is processing them.



When it hits LNVGY or unpredictably any other many possibilities, somehow it can't handle or at least skip them, instead crashing with no proper exception nor error code.



THE QUESTION



I'm trying to write a minimalistic wrapper script in BASH (eg. retryRB.sh ./robinbotter) which:




  1. Somehow monitors the live unbuffered output of robinbotter, using a regex or other method to detect when output of a line containing "Downloading instruments: [ " doesn't end with "]" before the program ends. In which case:


  2. Take the last symbol printed out (eg. LNVGY) which crashes the program, and append it to the bottom of file blacklist.sym. Like with



    echo $lastSymbol >> blacklist.sym



  3. Restart the program robinbotter, retaining its original command-line parameters: $@



I am familiar with tools like awk and sed, and would be open to building a short solution in Ruby if Bash doesn't cut it.










share|improve this question





























    1














    I've had a bit of shell scripting practice reading piped input from other programs, but am unsure how to approach this problem.



    THE BACKSTORY



    A program robinbotter whose internals I can't really fix/modify takes its input from files equities.sym and blacklist.sym, each simple text files containing one ticker symbol per line.



    When it runs okay, its output produces:



    ...
    Downloading instruments: [ AFL KELYB LFUS ]
    ...


    When it breaks due to internal bugs,



    ...
    Downloading instruments: [ AFL KELYB LFUS LNVGY


    and halts there, with no further output, yielding exit code 0 like in the okay case (unfortunately).



    The ticker symbols are printed out with slight delay--no newlines in between--while the program is processing them.



    When it hits LNVGY or unpredictably any other many possibilities, somehow it can't handle or at least skip them, instead crashing with no proper exception nor error code.



    THE QUESTION



    I'm trying to write a minimalistic wrapper script in BASH (eg. retryRB.sh ./robinbotter) which:




    1. Somehow monitors the live unbuffered output of robinbotter, using a regex or other method to detect when output of a line containing "Downloading instruments: [ " doesn't end with "]" before the program ends. In which case:


    2. Take the last symbol printed out (eg. LNVGY) which crashes the program, and append it to the bottom of file blacklist.sym. Like with



      echo $lastSymbol >> blacklist.sym



    3. Restart the program robinbotter, retaining its original command-line parameters: $@



    I am familiar with tools like awk and sed, and would be open to building a short solution in Ruby if Bash doesn't cut it.










    share|improve this question



























      1












      1








      1







      I've had a bit of shell scripting practice reading piped input from other programs, but am unsure how to approach this problem.



      THE BACKSTORY



      A program robinbotter whose internals I can't really fix/modify takes its input from files equities.sym and blacklist.sym, each simple text files containing one ticker symbol per line.



      When it runs okay, its output produces:



      ...
      Downloading instruments: [ AFL KELYB LFUS ]
      ...


      When it breaks due to internal bugs,



      ...
      Downloading instruments: [ AFL KELYB LFUS LNVGY


      and halts there, with no further output, yielding exit code 0 like in the okay case (unfortunately).



      The ticker symbols are printed out with slight delay--no newlines in between--while the program is processing them.



      When it hits LNVGY or unpredictably any other many possibilities, somehow it can't handle or at least skip them, instead crashing with no proper exception nor error code.



      THE QUESTION



      I'm trying to write a minimalistic wrapper script in BASH (eg. retryRB.sh ./robinbotter) which:




      1. Somehow monitors the live unbuffered output of robinbotter, using a regex or other method to detect when output of a line containing "Downloading instruments: [ " doesn't end with "]" before the program ends. In which case:


      2. Take the last symbol printed out (eg. LNVGY) which crashes the program, and append it to the bottom of file blacklist.sym. Like with



        echo $lastSymbol >> blacklist.sym



      3. Restart the program robinbotter, retaining its original command-line parameters: $@



      I am familiar with tools like awk and sed, and would be open to building a short solution in Ruby if Bash doesn't cut it.










      share|improve this question















      I've had a bit of shell scripting practice reading piped input from other programs, but am unsure how to approach this problem.



      THE BACKSTORY



      A program robinbotter whose internals I can't really fix/modify takes its input from files equities.sym and blacklist.sym, each simple text files containing one ticker symbol per line.



      When it runs okay, its output produces:



      ...
      Downloading instruments: [ AFL KELYB LFUS ]
      ...


      When it breaks due to internal bugs,



      ...
      Downloading instruments: [ AFL KELYB LFUS LNVGY


      and halts there, with no further output, yielding exit code 0 like in the okay case (unfortunately).



      The ticker symbols are printed out with slight delay--no newlines in between--while the program is processing them.



      When it hits LNVGY or unpredictably any other many possibilities, somehow it can't handle or at least skip them, instead crashing with no proper exception nor error code.



      THE QUESTION



      I'm trying to write a minimalistic wrapper script in BASH (eg. retryRB.sh ./robinbotter) which:




      1. Somehow monitors the live unbuffered output of robinbotter, using a regex or other method to detect when output of a line containing "Downloading instruments: [ " doesn't end with "]" before the program ends. In which case:


      2. Take the last symbol printed out (eg. LNVGY) which crashes the program, and append it to the bottom of file blacklist.sym. Like with



        echo $lastSymbol >> blacklist.sym



      3. Restart the program robinbotter, retaining its original command-line parameters: $@



      I am familiar with tools like awk and sed, and would be open to building a short solution in Ruby if Bash doesn't cut it.







      regex bash wrapper io-redirection






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 '18 at 16:04







      Marcos

















      asked Nov 20 '18 at 15:32









      MarcosMarcos

      2,75933057




      2,75933057
























          1 Answer
          1






          active

          oldest

          votes


















          2














          Here you have a Bash version of a code that imitates what your binary does.
          then you have a wrapper which logs when the apps successfully completes, and also, when it fails. On failure, it also appends the last item printed, as you can see in the images below ( in this case Im hard-coding a failure on Bomb! and Boom! but you get the idea):



          main.sh



          #!/bin/bash

          some=('Pera' 'Manzana' 'Frutilla' 'Durazno' 'Banana' 'Lechuga' 'Sandia' 'Papa' 'Melon' 'Milanesa' 'Bomb!' 'Boom!')

          printf 'Downloading instruments: [ '
          for (( i=1 ; i < 5 ; i++ )) {
          item=${some[$( shuf -i 0-$(( ${#some[@]} - 1 )) -n 1 )]}
          printf "$item"
          [[ $item == 'Bomb!' || $item == "Boom!" ]] && exit || printf "$item"
          [[ $i -lt 4 ]] && printf ' '
          }
          printf ' ]'


          wrapper.sh



          #!/bin/bash

          while :
          do
          res=$( ./main.sh )
          [[ ! "$res" =~ [[^]]*] ]] && printf "Failure : ${res##*[[ ]}" || printf "Success"
          printf 'n'
          sleep 1
          done


          enter image description here



          You can test these scripts and then put your binary in place of main.sh.
          Regards!






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            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: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            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
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53396384%2fshell-wrapper-to-restart-script-based-on-its-output%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









            2














            Here you have a Bash version of a code that imitates what your binary does.
            then you have a wrapper which logs when the apps successfully completes, and also, when it fails. On failure, it also appends the last item printed, as you can see in the images below ( in this case Im hard-coding a failure on Bomb! and Boom! but you get the idea):



            main.sh



            #!/bin/bash

            some=('Pera' 'Manzana' 'Frutilla' 'Durazno' 'Banana' 'Lechuga' 'Sandia' 'Papa' 'Melon' 'Milanesa' 'Bomb!' 'Boom!')

            printf 'Downloading instruments: [ '
            for (( i=1 ; i < 5 ; i++ )) {
            item=${some[$( shuf -i 0-$(( ${#some[@]} - 1 )) -n 1 )]}
            printf "$item"
            [[ $item == 'Bomb!' || $item == "Boom!" ]] && exit || printf "$item"
            [[ $i -lt 4 ]] && printf ' '
            }
            printf ' ]'


            wrapper.sh



            #!/bin/bash

            while :
            do
            res=$( ./main.sh )
            [[ ! "$res" =~ [[^]]*] ]] && printf "Failure : ${res##*[[ ]}" || printf "Success"
            printf 'n'
            sleep 1
            done


            enter image description here



            You can test these scripts and then put your binary in place of main.sh.
            Regards!






            share|improve this answer


























              2














              Here you have a Bash version of a code that imitates what your binary does.
              then you have a wrapper which logs when the apps successfully completes, and also, when it fails. On failure, it also appends the last item printed, as you can see in the images below ( in this case Im hard-coding a failure on Bomb! and Boom! but you get the idea):



              main.sh



              #!/bin/bash

              some=('Pera' 'Manzana' 'Frutilla' 'Durazno' 'Banana' 'Lechuga' 'Sandia' 'Papa' 'Melon' 'Milanesa' 'Bomb!' 'Boom!')

              printf 'Downloading instruments: [ '
              for (( i=1 ; i < 5 ; i++ )) {
              item=${some[$( shuf -i 0-$(( ${#some[@]} - 1 )) -n 1 )]}
              printf "$item"
              [[ $item == 'Bomb!' || $item == "Boom!" ]] && exit || printf "$item"
              [[ $i -lt 4 ]] && printf ' '
              }
              printf ' ]'


              wrapper.sh



              #!/bin/bash

              while :
              do
              res=$( ./main.sh )
              [[ ! "$res" =~ [[^]]*] ]] && printf "Failure : ${res##*[[ ]}" || printf "Success"
              printf 'n'
              sleep 1
              done


              enter image description here



              You can test these scripts and then put your binary in place of main.sh.
              Regards!






              share|improve this answer
























                2












                2








                2






                Here you have a Bash version of a code that imitates what your binary does.
                then you have a wrapper which logs when the apps successfully completes, and also, when it fails. On failure, it also appends the last item printed, as you can see in the images below ( in this case Im hard-coding a failure on Bomb! and Boom! but you get the idea):



                main.sh



                #!/bin/bash

                some=('Pera' 'Manzana' 'Frutilla' 'Durazno' 'Banana' 'Lechuga' 'Sandia' 'Papa' 'Melon' 'Milanesa' 'Bomb!' 'Boom!')

                printf 'Downloading instruments: [ '
                for (( i=1 ; i < 5 ; i++ )) {
                item=${some[$( shuf -i 0-$(( ${#some[@]} - 1 )) -n 1 )]}
                printf "$item"
                [[ $item == 'Bomb!' || $item == "Boom!" ]] && exit || printf "$item"
                [[ $i -lt 4 ]] && printf ' '
                }
                printf ' ]'


                wrapper.sh



                #!/bin/bash

                while :
                do
                res=$( ./main.sh )
                [[ ! "$res" =~ [[^]]*] ]] && printf "Failure : ${res##*[[ ]}" || printf "Success"
                printf 'n'
                sleep 1
                done


                enter image description here



                You can test these scripts and then put your binary in place of main.sh.
                Regards!






                share|improve this answer












                Here you have a Bash version of a code that imitates what your binary does.
                then you have a wrapper which logs when the apps successfully completes, and also, when it fails. On failure, it also appends the last item printed, as you can see in the images below ( in this case Im hard-coding a failure on Bomb! and Boom! but you get the idea):



                main.sh



                #!/bin/bash

                some=('Pera' 'Manzana' 'Frutilla' 'Durazno' 'Banana' 'Lechuga' 'Sandia' 'Papa' 'Melon' 'Milanesa' 'Bomb!' 'Boom!')

                printf 'Downloading instruments: [ '
                for (( i=1 ; i < 5 ; i++ )) {
                item=${some[$( shuf -i 0-$(( ${#some[@]} - 1 )) -n 1 )]}
                printf "$item"
                [[ $item == 'Bomb!' || $item == "Boom!" ]] && exit || printf "$item"
                [[ $i -lt 4 ]] && printf ' '
                }
                printf ' ]'


                wrapper.sh



                #!/bin/bash

                while :
                do
                res=$( ./main.sh )
                [[ ! "$res" =~ [[^]]*] ]] && printf "Failure : ${res##*[[ ]}" || printf "Success"
                printf 'n'
                sleep 1
                done


                enter image description here



                You can test these scripts and then put your binary in place of main.sh.
                Regards!







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 20 '18 at 16:49









                Matias BarriosMatias Barrios

                1,530316




                1,530316






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • 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%2fstackoverflow.com%2fquestions%2f53396384%2fshell-wrapper-to-restart-script-based-on-its-output%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

                    "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                    Alcedinidae

                    Origin of the phrase “under your belt”?