Preventing parameter expansion multiple times

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











up vote
2
down vote

favorite












I have a code that is writing a script which is writing another script:



 cat > step1.sh <<-EOF 
*other commands*
cat > step2.sh <<-EOF4
if [ -f PM.log ]; then
awk '/testphrase/ f=1;r=""; next f && /testphrase2/f=0f r=(r=="")? $0: r RS $0 END print r ' PM.log > tmp1.log
checkfree=($(cat tmp1.log))
wait
sed -i '$ d' tmp$mold.log
wait
fi
EOF4
qsub step2.sh
EOF
qsub step1.sh


I am trying to prevent parameter expansion with the "$0" in line 5 of the code as well as with line 6 of the code ($(cat tmp1.log)).
I am aware that using "" before the $ would prevent the parameter expansion once:



$0 


However, because this script is written by another script, I don't know how to manipulate to prevent double and triple expansions as each script is being written.



I also know that you can edit the end of file token but there are other parameters (such as mold in line 8) within the file that I DO want to expand so I can't do that either.



How can I stop these particular terms from being expanded every time?










share|improve this question























  • It looks as if EOF4 is the ending of an internal here-document, but I can't see where that here-document starts...
    – Kusalananda
    Feb 10 at 8:06















up vote
2
down vote

favorite












I have a code that is writing a script which is writing another script:



 cat > step1.sh <<-EOF 
*other commands*
cat > step2.sh <<-EOF4
if [ -f PM.log ]; then
awk '/testphrase/ f=1;r=""; next f && /testphrase2/f=0f r=(r=="")? $0: r RS $0 END print r ' PM.log > tmp1.log
checkfree=($(cat tmp1.log))
wait
sed -i '$ d' tmp$mold.log
wait
fi
EOF4
qsub step2.sh
EOF
qsub step1.sh


I am trying to prevent parameter expansion with the "$0" in line 5 of the code as well as with line 6 of the code ($(cat tmp1.log)).
I am aware that using "" before the $ would prevent the parameter expansion once:



$0 


However, because this script is written by another script, I don't know how to manipulate to prevent double and triple expansions as each script is being written.



I also know that you can edit the end of file token but there are other parameters (such as mold in line 8) within the file that I DO want to expand so I can't do that either.



How can I stop these particular terms from being expanded every time?










share|improve this question























  • It looks as if EOF4 is the ending of an internal here-document, but I can't see where that here-document starts...
    – Kusalananda
    Feb 10 at 8:06













up vote
2
down vote

favorite









up vote
2
down vote

favorite











I have a code that is writing a script which is writing another script:



 cat > step1.sh <<-EOF 
*other commands*
cat > step2.sh <<-EOF4
if [ -f PM.log ]; then
awk '/testphrase/ f=1;r=""; next f && /testphrase2/f=0f r=(r=="")? $0: r RS $0 END print r ' PM.log > tmp1.log
checkfree=($(cat tmp1.log))
wait
sed -i '$ d' tmp$mold.log
wait
fi
EOF4
qsub step2.sh
EOF
qsub step1.sh


I am trying to prevent parameter expansion with the "$0" in line 5 of the code as well as with line 6 of the code ($(cat tmp1.log)).
I am aware that using "" before the $ would prevent the parameter expansion once:



$0 


However, because this script is written by another script, I don't know how to manipulate to prevent double and triple expansions as each script is being written.



I also know that you can edit the end of file token but there are other parameters (such as mold in line 8) within the file that I DO want to expand so I can't do that either.



How can I stop these particular terms from being expanded every time?










share|improve this question















I have a code that is writing a script which is writing another script:



 cat > step1.sh <<-EOF 
*other commands*
cat > step2.sh <<-EOF4
if [ -f PM.log ]; then
awk '/testphrase/ f=1;r=""; next f && /testphrase2/f=0f r=(r=="")? $0: r RS $0 END print r ' PM.log > tmp1.log
checkfree=($(cat tmp1.log))
wait
sed -i '$ d' tmp$mold.log
wait
fi
EOF4
qsub step2.sh
EOF
qsub step1.sh


I am trying to prevent parameter expansion with the "$0" in line 5 of the code as well as with line 6 of the code ($(cat tmp1.log)).
I am aware that using "" before the $ would prevent the parameter expansion once:



$0 


However, because this script is written by another script, I don't know how to manipulate to prevent double and triple expansions as each script is being written.



I also know that you can edit the end of file token but there are other parameters (such as mold in line 8) within the file that I DO want to expand so I can't do that either.



How can I stop these particular terms from being expanded every time?







shell scripting parameter






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 10 at 14:57

























asked Oct 9 '17 at 16:57









A. B

224




224











  • It looks as if EOF4 is the ending of an internal here-document, but I can't see where that here-document starts...
    – Kusalananda
    Feb 10 at 8:06

















  • It looks as if EOF4 is the ending of an internal here-document, but I can't see where that here-document starts...
    – Kusalananda
    Feb 10 at 8:06
















It looks as if EOF4 is the ending of an internal here-document, but I can't see where that here-document starts...
– Kusalananda
Feb 10 at 8:06





It looks as if EOF4 is the ending of an internal here-document, but I can't see where that here-document starts...
– Kusalananda
Feb 10 at 8:06











2 Answers
2






active

oldest

votes

















up vote
1
down vote













Quoting the end of line token does this. Quote it in your first line:



cat > run1.sh <<-"EOF"





share|improve this answer




















  • I can't do this because there are other parameters in the script that I DO want to expand. Quoting the end of line token would prevent all the expansions.
    – A. B
    Feb 10 at 0:45

















up vote
0
down vote













Consider the following example script (all indentations are literal tabs):



#!/bin/sh

var='hello'

cat >script1.sh <<-END_SCRIPT1
#!/bin/sh
echo 'This is script1 ($var)'
printf '$0 is "%s"n' "$0"

cat >script2.sh <<END_SCRIPT2
#!/bin/sh
echo 'This is script2 ($var)'
printf '\$0 is "%s"n' "\$0"
END_SCRIPT2
END_SCRIPT1


In the outer here-document, we need to escape the $ in "$0" as "$0" to not have it expanded by the script writing the document.



To be able to write the inner here-document, we need to make sure that the outer here-document is writing "$0". The backslash needs to be escaped as \, which turns the string into "\$0".



The script above writes script1.sh as



#!/bin/sh
echo 'This is script2 (hello)'
printf '$0 is "%s"n' "$0"
END_SCRIPT2
printf '$0 is "%s"n' "$0"


Running this outputs



This is script1 (hello)
$0 is "script1.sh"


... and script2.sh is created:



#!/bin/sh
echo 'This is script2 (hello)'
printf '$0 is "%s"n' "$0"



Notice that the inner here-document is created using <<END_SCRIPT2, not <<-END_SCRIPT2. Any leading tabs will be eaten up when the outer here-document is created (as evident when looking at script1.sh).




As an aside, you don't need the two calls to wait in your code since you don't seem to run any background processes.



Your awk code is also faulty as the f && /testphrase2/ condition would never be true. If /testphrase/ matches, then next is executed, forcing the code to continue with the next line of input. If /testphrase/ does not match, then /testphrase2/ won't match either.



Your could try this instead:



awk '!f && /testphrase/ f = 1; r = "" 
f && /testphrase2/ f = 0
f r = (r == "" ? $0 : r RS $0)
END print r '





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%2f397068%2fpreventing-parameter-expansion-multiple-times%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    Quoting the end of line token does this. Quote it in your first line:



    cat > run1.sh <<-"EOF"





    share|improve this answer




















    • I can't do this because there are other parameters in the script that I DO want to expand. Quoting the end of line token would prevent all the expansions.
      – A. B
      Feb 10 at 0:45














    up vote
    1
    down vote













    Quoting the end of line token does this. Quote it in your first line:



    cat > run1.sh <<-"EOF"





    share|improve this answer




















    • I can't do this because there are other parameters in the script that I DO want to expand. Quoting the end of line token would prevent all the expansions.
      – A. B
      Feb 10 at 0:45












    up vote
    1
    down vote










    up vote
    1
    down vote









    Quoting the end of line token does this. Quote it in your first line:



    cat > run1.sh <<-"EOF"





    share|improve this answer












    Quoting the end of line token does this. Quote it in your first line:



    cat > run1.sh <<-"EOF"






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Oct 9 '17 at 17:06









    Tomasz

    8,08752560




    8,08752560











    • I can't do this because there are other parameters in the script that I DO want to expand. Quoting the end of line token would prevent all the expansions.
      – A. B
      Feb 10 at 0:45
















    • I can't do this because there are other parameters in the script that I DO want to expand. Quoting the end of line token would prevent all the expansions.
      – A. B
      Feb 10 at 0:45















    I can't do this because there are other parameters in the script that I DO want to expand. Quoting the end of line token would prevent all the expansions.
    – A. B
    Feb 10 at 0:45




    I can't do this because there are other parameters in the script that I DO want to expand. Quoting the end of line token would prevent all the expansions.
    – A. B
    Feb 10 at 0:45












    up vote
    0
    down vote













    Consider the following example script (all indentations are literal tabs):



    #!/bin/sh

    var='hello'

    cat >script1.sh <<-END_SCRIPT1
    #!/bin/sh
    echo 'This is script1 ($var)'
    printf '$0 is "%s"n' "$0"

    cat >script2.sh <<END_SCRIPT2
    #!/bin/sh
    echo 'This is script2 ($var)'
    printf '\$0 is "%s"n' "\$0"
    END_SCRIPT2
    END_SCRIPT1


    In the outer here-document, we need to escape the $ in "$0" as "$0" to not have it expanded by the script writing the document.



    To be able to write the inner here-document, we need to make sure that the outer here-document is writing "$0". The backslash needs to be escaped as \, which turns the string into "\$0".



    The script above writes script1.sh as



    #!/bin/sh
    echo 'This is script2 (hello)'
    printf '$0 is "%s"n' "$0"
    END_SCRIPT2
    printf '$0 is "%s"n' "$0"


    Running this outputs



    This is script1 (hello)
    $0 is "script1.sh"


    ... and script2.sh is created:



    #!/bin/sh
    echo 'This is script2 (hello)'
    printf '$0 is "%s"n' "$0"



    Notice that the inner here-document is created using <<END_SCRIPT2, not <<-END_SCRIPT2. Any leading tabs will be eaten up when the outer here-document is created (as evident when looking at script1.sh).




    As an aside, you don't need the two calls to wait in your code since you don't seem to run any background processes.



    Your awk code is also faulty as the f && /testphrase2/ condition would never be true. If /testphrase/ matches, then next is executed, forcing the code to continue with the next line of input. If /testphrase/ does not match, then /testphrase2/ won't match either.



    Your could try this instead:



    awk '!f && /testphrase/ f = 1; r = "" 
    f && /testphrase2/ f = 0
    f r = (r == "" ? $0 : r RS $0)
    END print r '





    share|improve this answer


























      up vote
      0
      down vote













      Consider the following example script (all indentations are literal tabs):



      #!/bin/sh

      var='hello'

      cat >script1.sh <<-END_SCRIPT1
      #!/bin/sh
      echo 'This is script1 ($var)'
      printf '$0 is "%s"n' "$0"

      cat >script2.sh <<END_SCRIPT2
      #!/bin/sh
      echo 'This is script2 ($var)'
      printf '\$0 is "%s"n' "\$0"
      END_SCRIPT2
      END_SCRIPT1


      In the outer here-document, we need to escape the $ in "$0" as "$0" to not have it expanded by the script writing the document.



      To be able to write the inner here-document, we need to make sure that the outer here-document is writing "$0". The backslash needs to be escaped as \, which turns the string into "\$0".



      The script above writes script1.sh as



      #!/bin/sh
      echo 'This is script2 (hello)'
      printf '$0 is "%s"n' "$0"
      END_SCRIPT2
      printf '$0 is "%s"n' "$0"


      Running this outputs



      This is script1 (hello)
      $0 is "script1.sh"


      ... and script2.sh is created:



      #!/bin/sh
      echo 'This is script2 (hello)'
      printf '$0 is "%s"n' "$0"



      Notice that the inner here-document is created using <<END_SCRIPT2, not <<-END_SCRIPT2. Any leading tabs will be eaten up when the outer here-document is created (as evident when looking at script1.sh).




      As an aside, you don't need the two calls to wait in your code since you don't seem to run any background processes.



      Your awk code is also faulty as the f && /testphrase2/ condition would never be true. If /testphrase/ matches, then next is executed, forcing the code to continue with the next line of input. If /testphrase/ does not match, then /testphrase2/ won't match either.



      Your could try this instead:



      awk '!f && /testphrase/ f = 1; r = "" 
      f && /testphrase2/ f = 0
      f r = (r == "" ? $0 : r RS $0)
      END print r '





      share|improve this answer
























        up vote
        0
        down vote










        up vote
        0
        down vote









        Consider the following example script (all indentations are literal tabs):



        #!/bin/sh

        var='hello'

        cat >script1.sh <<-END_SCRIPT1
        #!/bin/sh
        echo 'This is script1 ($var)'
        printf '$0 is "%s"n' "$0"

        cat >script2.sh <<END_SCRIPT2
        #!/bin/sh
        echo 'This is script2 ($var)'
        printf '\$0 is "%s"n' "\$0"
        END_SCRIPT2
        END_SCRIPT1


        In the outer here-document, we need to escape the $ in "$0" as "$0" to not have it expanded by the script writing the document.



        To be able to write the inner here-document, we need to make sure that the outer here-document is writing "$0". The backslash needs to be escaped as \, which turns the string into "\$0".



        The script above writes script1.sh as



        #!/bin/sh
        echo 'This is script2 (hello)'
        printf '$0 is "%s"n' "$0"
        END_SCRIPT2
        printf '$0 is "%s"n' "$0"


        Running this outputs



        This is script1 (hello)
        $0 is "script1.sh"


        ... and script2.sh is created:



        #!/bin/sh
        echo 'This is script2 (hello)'
        printf '$0 is "%s"n' "$0"



        Notice that the inner here-document is created using <<END_SCRIPT2, not <<-END_SCRIPT2. Any leading tabs will be eaten up when the outer here-document is created (as evident when looking at script1.sh).




        As an aside, you don't need the two calls to wait in your code since you don't seem to run any background processes.



        Your awk code is also faulty as the f && /testphrase2/ condition would never be true. If /testphrase/ matches, then next is executed, forcing the code to continue with the next line of input. If /testphrase/ does not match, then /testphrase2/ won't match either.



        Your could try this instead:



        awk '!f && /testphrase/ f = 1; r = "" 
        f && /testphrase2/ f = 0
        f r = (r == "" ? $0 : r RS $0)
        END print r '





        share|improve this answer














        Consider the following example script (all indentations are literal tabs):



        #!/bin/sh

        var='hello'

        cat >script1.sh <<-END_SCRIPT1
        #!/bin/sh
        echo 'This is script1 ($var)'
        printf '$0 is "%s"n' "$0"

        cat >script2.sh <<END_SCRIPT2
        #!/bin/sh
        echo 'This is script2 ($var)'
        printf '\$0 is "%s"n' "\$0"
        END_SCRIPT2
        END_SCRIPT1


        In the outer here-document, we need to escape the $ in "$0" as "$0" to not have it expanded by the script writing the document.



        To be able to write the inner here-document, we need to make sure that the outer here-document is writing "$0". The backslash needs to be escaped as \, which turns the string into "\$0".



        The script above writes script1.sh as



        #!/bin/sh
        echo 'This is script2 (hello)'
        printf '$0 is "%s"n' "$0"
        END_SCRIPT2
        printf '$0 is "%s"n' "$0"


        Running this outputs



        This is script1 (hello)
        $0 is "script1.sh"


        ... and script2.sh is created:



        #!/bin/sh
        echo 'This is script2 (hello)'
        printf '$0 is "%s"n' "$0"



        Notice that the inner here-document is created using <<END_SCRIPT2, not <<-END_SCRIPT2. Any leading tabs will be eaten up when the outer here-document is created (as evident when looking at script1.sh).




        As an aside, you don't need the two calls to wait in your code since you don't seem to run any background processes.



        Your awk code is also faulty as the f && /testphrase2/ condition would never be true. If /testphrase/ matches, then next is executed, forcing the code to continue with the next line of input. If /testphrase/ does not match, then /testphrase2/ won't match either.



        Your could try this instead:



        awk '!f && /testphrase/ f = 1; r = "" 
        f && /testphrase2/ f = 0
        f r = (r == "" ? $0 : r RS $0)
        END print r '






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited May 5 at 22:19

























        answered May 5 at 22:02









        Kusalananda

        105k14209326




        105k14209326



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f397068%2fpreventing-parameter-expansion-multiple-times%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