Conditionally dry run a command block with BASH heredoc












2















I wanted to have something as simple as that in my script:



set_dryrun()
{
BEGIN='cat << EOF'
END='EOF'
}

[ "$1" == "-dryrun" ] && set_dryrun

${BEGIN}
ls -l
${END}


So that if I run it with the -dryrun parameter, commands between ${BEGIN} and ${END} would be just printed, but not executed.



Everything goes well if I run without the -dryrun flag:



$ ./dryrun_opt 
-rwxrwxr-x 1 arantesj arantesj 160 jan 25 17:15 dryrun_opt


With -dryrun, though, I see:



$ ./dryrun_opt -dryrun
cat: '<<': No such file or directory
cat: EOF: No such file or directory
-rwxrwxr-x 1 arantesj arantesj 160 jan 25 17:16 dryrun_opt
./dryrun_opt: line 14: EOF: command not found


Well... I am not replacing (expanding?) my heredoc correctly, I know, but how should I, if possible?



Thanks in advance.










share|improve this question



























    2















    I wanted to have something as simple as that in my script:



    set_dryrun()
    {
    BEGIN='cat << EOF'
    END='EOF'
    }

    [ "$1" == "-dryrun" ] && set_dryrun

    ${BEGIN}
    ls -l
    ${END}


    So that if I run it with the -dryrun parameter, commands between ${BEGIN} and ${END} would be just printed, but not executed.



    Everything goes well if I run without the -dryrun flag:



    $ ./dryrun_opt 
    -rwxrwxr-x 1 arantesj arantesj 160 jan 25 17:15 dryrun_opt


    With -dryrun, though, I see:



    $ ./dryrun_opt -dryrun
    cat: '<<': No such file or directory
    cat: EOF: No such file or directory
    -rwxrwxr-x 1 arantesj arantesj 160 jan 25 17:16 dryrun_opt
    ./dryrun_opt: line 14: EOF: command not found


    Well... I am not replacing (expanding?) my heredoc correctly, I know, but how should I, if possible?



    Thanks in advance.










    share|improve this question

























      2












      2








      2








      I wanted to have something as simple as that in my script:



      set_dryrun()
      {
      BEGIN='cat << EOF'
      END='EOF'
      }

      [ "$1" == "-dryrun" ] && set_dryrun

      ${BEGIN}
      ls -l
      ${END}


      So that if I run it with the -dryrun parameter, commands between ${BEGIN} and ${END} would be just printed, but not executed.



      Everything goes well if I run without the -dryrun flag:



      $ ./dryrun_opt 
      -rwxrwxr-x 1 arantesj arantesj 160 jan 25 17:15 dryrun_opt


      With -dryrun, though, I see:



      $ ./dryrun_opt -dryrun
      cat: '<<': No such file or directory
      cat: EOF: No such file or directory
      -rwxrwxr-x 1 arantesj arantesj 160 jan 25 17:16 dryrun_opt
      ./dryrun_opt: line 14: EOF: command not found


      Well... I am not replacing (expanding?) my heredoc correctly, I know, but how should I, if possible?



      Thanks in advance.










      share|improve this question














      I wanted to have something as simple as that in my script:



      set_dryrun()
      {
      BEGIN='cat << EOF'
      END='EOF'
      }

      [ "$1" == "-dryrun" ] && set_dryrun

      ${BEGIN}
      ls -l
      ${END}


      So that if I run it with the -dryrun parameter, commands between ${BEGIN} and ${END} would be just printed, but not executed.



      Everything goes well if I run without the -dryrun flag:



      $ ./dryrun_opt 
      -rwxrwxr-x 1 arantesj arantesj 160 jan 25 17:15 dryrun_opt


      With -dryrun, though, I see:



      $ ./dryrun_opt -dryrun
      cat: '<<': No such file or directory
      cat: EOF: No such file or directory
      -rwxrwxr-x 1 arantesj arantesj 160 jan 25 17:16 dryrun_opt
      ./dryrun_opt: line 14: EOF: command not found


      Well... I am not replacing (expanding?) my heredoc correctly, I know, but how should I, if possible?



      Thanks in advance.







      bash bash-scripting debug






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 25 at 16:20









      j4xj4x

      1175




      1175






















          1 Answer
          1






          active

          oldest

          votes


















          1














          This works for me:



          set_dryrun()
          {
          BEGIN='cat << EOF'
          END='EOF'
          }

          [ "$1" == "-dryrun" ] && set_dryrun

          eval "${BEGIN}
          ls -l
          ${END}"


          However, I suspect that this whole approach will break at some point, when the heredoc becomes more complex than 'ls -l'. If there were quoting or escaping going on inside that, it would need to be done carefully or an approach this simple is likely to break.



          Borrowing from the wisdom of this Stack Overflow thread, you might find this approach interesting to consider:



          if [ "$1" == "-dryrun" ]
          then
          CMD="cat"
          else
          CMD="bash" # your preferred shell here
          fi

          WORK="$(cat <<'EOF'
          # abc'asdf"
          # $(dont-execute-this)
          # foo"bar"''
          ls -l
          echo "$PATH"
          echo "$PATH"
          EOF
          )"

          $CMD <<< "$WORK"





          share|improve this answer


























          • Great! It worked fine for my current needs, but I will keep in mind (and text) your advice. Thank you @Jim-L.

            – j4x
            Jan 28 at 6:42












          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "3"
          };
          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%2fsuperuser.com%2fquestions%2f1398453%2fconditionally-dry-run-a-command-block-with-bash-heredoc%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









          1














          This works for me:



          set_dryrun()
          {
          BEGIN='cat << EOF'
          END='EOF'
          }

          [ "$1" == "-dryrun" ] && set_dryrun

          eval "${BEGIN}
          ls -l
          ${END}"


          However, I suspect that this whole approach will break at some point, when the heredoc becomes more complex than 'ls -l'. If there were quoting or escaping going on inside that, it would need to be done carefully or an approach this simple is likely to break.



          Borrowing from the wisdom of this Stack Overflow thread, you might find this approach interesting to consider:



          if [ "$1" == "-dryrun" ]
          then
          CMD="cat"
          else
          CMD="bash" # your preferred shell here
          fi

          WORK="$(cat <<'EOF'
          # abc'asdf"
          # $(dont-execute-this)
          # foo"bar"''
          ls -l
          echo "$PATH"
          echo "$PATH"
          EOF
          )"

          $CMD <<< "$WORK"





          share|improve this answer


























          • Great! It worked fine for my current needs, but I will keep in mind (and text) your advice. Thank you @Jim-L.

            – j4x
            Jan 28 at 6:42
















          1














          This works for me:



          set_dryrun()
          {
          BEGIN='cat << EOF'
          END='EOF'
          }

          [ "$1" == "-dryrun" ] && set_dryrun

          eval "${BEGIN}
          ls -l
          ${END}"


          However, I suspect that this whole approach will break at some point, when the heredoc becomes more complex than 'ls -l'. If there were quoting or escaping going on inside that, it would need to be done carefully or an approach this simple is likely to break.



          Borrowing from the wisdom of this Stack Overflow thread, you might find this approach interesting to consider:



          if [ "$1" == "-dryrun" ]
          then
          CMD="cat"
          else
          CMD="bash" # your preferred shell here
          fi

          WORK="$(cat <<'EOF'
          # abc'asdf"
          # $(dont-execute-this)
          # foo"bar"''
          ls -l
          echo "$PATH"
          echo "$PATH"
          EOF
          )"

          $CMD <<< "$WORK"





          share|improve this answer


























          • Great! It worked fine for my current needs, but I will keep in mind (and text) your advice. Thank you @Jim-L.

            – j4x
            Jan 28 at 6:42














          1












          1








          1







          This works for me:



          set_dryrun()
          {
          BEGIN='cat << EOF'
          END='EOF'
          }

          [ "$1" == "-dryrun" ] && set_dryrun

          eval "${BEGIN}
          ls -l
          ${END}"


          However, I suspect that this whole approach will break at some point, when the heredoc becomes more complex than 'ls -l'. If there were quoting or escaping going on inside that, it would need to be done carefully or an approach this simple is likely to break.



          Borrowing from the wisdom of this Stack Overflow thread, you might find this approach interesting to consider:



          if [ "$1" == "-dryrun" ]
          then
          CMD="cat"
          else
          CMD="bash" # your preferred shell here
          fi

          WORK="$(cat <<'EOF'
          # abc'asdf"
          # $(dont-execute-this)
          # foo"bar"''
          ls -l
          echo "$PATH"
          echo "$PATH"
          EOF
          )"

          $CMD <<< "$WORK"





          share|improve this answer















          This works for me:



          set_dryrun()
          {
          BEGIN='cat << EOF'
          END='EOF'
          }

          [ "$1" == "-dryrun" ] && set_dryrun

          eval "${BEGIN}
          ls -l
          ${END}"


          However, I suspect that this whole approach will break at some point, when the heredoc becomes more complex than 'ls -l'. If there were quoting or escaping going on inside that, it would need to be done carefully or an approach this simple is likely to break.



          Borrowing from the wisdom of this Stack Overflow thread, you might find this approach interesting to consider:



          if [ "$1" == "-dryrun" ]
          then
          CMD="cat"
          else
          CMD="bash" # your preferred shell here
          fi

          WORK="$(cat <<'EOF'
          # abc'asdf"
          # $(dont-execute-this)
          # foo"bar"''
          ls -l
          echo "$PATH"
          echo "$PATH"
          EOF
          )"

          $CMD <<< "$WORK"






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 25 at 18:20

























          answered Jan 25 at 17:46









          Jim L.Jim L.

          44117




          44117













          • Great! It worked fine for my current needs, but I will keep in mind (and text) your advice. Thank you @Jim-L.

            – j4x
            Jan 28 at 6:42



















          • Great! It worked fine for my current needs, but I will keep in mind (and text) your advice. Thank you @Jim-L.

            – j4x
            Jan 28 at 6:42

















          Great! It worked fine for my current needs, but I will keep in mind (and text) your advice. Thank you @Jim-L.

          – j4x
          Jan 28 at 6:42





          Great! It worked fine for my current needs, but I will keep in mind (and text) your advice. Thank you @Jim-L.

          – j4x
          Jan 28 at 6:42


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Super User!


          • 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%2fsuperuser.com%2fquestions%2f1398453%2fconditionally-dry-run-a-command-block-with-bash-heredoc%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

          Paul Cézanne

          UIScrollView CustomStickyHeader Resize height generates problems when scroll is too fast

          Angular material date-picker (MatDatepicker) auto completes the date on focus out