My bash script is not making new files











up vote
0
down vote

favorite












Im trying to iterate over a list containing urls, and download the content. The content is piped in json_pp to beatify it.



But the problem is that it only generates loot_0.json! And keeps overwriting the content.



#!/bin/bash
COUNTER=0
cat links.txt | while read line; do #links.txt
PAGE=$(curl -s $line)
echo $PAGE | json_pp
done > loot/loot_$((COUNTER++)).json


I have also tried



#!/bin/bash
COUNTER=0
cat links.txt | while read line; do #links.txt
PAGE=$(curl -s $line)
echo $PAGE | json_pp > loot/loot_$((COUNTER++)).json
done


Expected behavior should be files



loot_1.json
loot_2.json
loot_3.json
..









share|improve this question


















  • 1




    Your first example will not work since COUNTER is incremented outside of the while loop. However, it should contain all pages that were brought in by curl. The second loop should produce individual files, one for each page brought in by curl. That should be right. I tested it on my Ubuntu 18.04 system and it worked. I did not have the "| json_pp", but I had everything else.
    – Lewis M
    Nov 28 at 19:19















up vote
0
down vote

favorite












Im trying to iterate over a list containing urls, and download the content. The content is piped in json_pp to beatify it.



But the problem is that it only generates loot_0.json! And keeps overwriting the content.



#!/bin/bash
COUNTER=0
cat links.txt | while read line; do #links.txt
PAGE=$(curl -s $line)
echo $PAGE | json_pp
done > loot/loot_$((COUNTER++)).json


I have also tried



#!/bin/bash
COUNTER=0
cat links.txt | while read line; do #links.txt
PAGE=$(curl -s $line)
echo $PAGE | json_pp > loot/loot_$((COUNTER++)).json
done


Expected behavior should be files



loot_1.json
loot_2.json
loot_3.json
..









share|improve this question


















  • 1




    Your first example will not work since COUNTER is incremented outside of the while loop. However, it should contain all pages that were brought in by curl. The second loop should produce individual files, one for each page brought in by curl. That should be right. I tested it on my Ubuntu 18.04 system and it worked. I did not have the "| json_pp", but I had everything else.
    – Lewis M
    Nov 28 at 19:19













up vote
0
down vote

favorite









up vote
0
down vote

favorite











Im trying to iterate over a list containing urls, and download the content. The content is piped in json_pp to beatify it.



But the problem is that it only generates loot_0.json! And keeps overwriting the content.



#!/bin/bash
COUNTER=0
cat links.txt | while read line; do #links.txt
PAGE=$(curl -s $line)
echo $PAGE | json_pp
done > loot/loot_$((COUNTER++)).json


I have also tried



#!/bin/bash
COUNTER=0
cat links.txt | while read line; do #links.txt
PAGE=$(curl -s $line)
echo $PAGE | json_pp > loot/loot_$((COUNTER++)).json
done


Expected behavior should be files



loot_1.json
loot_2.json
loot_3.json
..









share|improve this question













Im trying to iterate over a list containing urls, and download the content. The content is piped in json_pp to beatify it.



But the problem is that it only generates loot_0.json! And keeps overwriting the content.



#!/bin/bash
COUNTER=0
cat links.txt | while read line; do #links.txt
PAGE=$(curl -s $line)
echo $PAGE | json_pp
done > loot/loot_$((COUNTER++)).json


I have also tried



#!/bin/bash
COUNTER=0
cat links.txt | while read line; do #links.txt
PAGE=$(curl -s $line)
echo $PAGE | json_pp > loot/loot_$((COUNTER++)).json
done


Expected behavior should be files



loot_1.json
loot_2.json
loot_3.json
..






bash






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 28 at 18:39









Adam

19610




19610








  • 1




    Your first example will not work since COUNTER is incremented outside of the while loop. However, it should contain all pages that were brought in by curl. The second loop should produce individual files, one for each page brought in by curl. That should be right. I tested it on my Ubuntu 18.04 system and it worked. I did not have the "| json_pp", but I had everything else.
    – Lewis M
    Nov 28 at 19:19














  • 1




    Your first example will not work since COUNTER is incremented outside of the while loop. However, it should contain all pages that were brought in by curl. The second loop should produce individual files, one for each page brought in by curl. That should be right. I tested it on my Ubuntu 18.04 system and it worked. I did not have the "| json_pp", but I had everything else.
    – Lewis M
    Nov 28 at 19:19








1




1




Your first example will not work since COUNTER is incremented outside of the while loop. However, it should contain all pages that were brought in by curl. The second loop should produce individual files, one for each page brought in by curl. That should be right. I tested it on my Ubuntu 18.04 system and it worked. I did not have the "| json_pp", but I had everything else.
– Lewis M
Nov 28 at 19:19




Your first example will not work since COUNTER is incremented outside of the while loop. However, it should contain all pages that were brought in by curl. The second loop should produce individual files, one for each page brought in by curl. That should be right. I tested it on my Ubuntu 18.04 system and it worked. I did not have the "| json_pp", but I had everything else.
– Lewis M
Nov 28 at 19:19










1 Answer
1






active

oldest

votes

















up vote
0
down vote













The following shellscript works for me. I have problems with json_pp, it complains about malformed JSON string, neither array, object, number, string or atom.



#!/bin/bash

counter=0
while read line;
do
# wget "$line" -O "${line##*/}$((counter++))"
page=$(curl -s "$line")
echo "$page" > "loot_$((counter++))"
done < links.txt



  • I prefer lower case variables (to decrease the risk of conflict with already existing environment variables).


  • I redirect from the input file (and avoid calling cat)


  • I update counter inside the loop


  • I use local variables loot_... for testing, but you can keep the subdirectory in the path if you wish.


  • Please notice that I have quoted, "...", the variables when used in the script.







share|improve this answer





















    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "89"
    };
    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: 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%2faskubuntu.com%2fquestions%2f1096895%2fmy-bash-script-is-not-making-new-files%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








    up vote
    0
    down vote













    The following shellscript works for me. I have problems with json_pp, it complains about malformed JSON string, neither array, object, number, string or atom.



    #!/bin/bash

    counter=0
    while read line;
    do
    # wget "$line" -O "${line##*/}$((counter++))"
    page=$(curl -s "$line")
    echo "$page" > "loot_$((counter++))"
    done < links.txt



    • I prefer lower case variables (to decrease the risk of conflict with already existing environment variables).


    • I redirect from the input file (and avoid calling cat)


    • I update counter inside the loop


    • I use local variables loot_... for testing, but you can keep the subdirectory in the path if you wish.


    • Please notice that I have quoted, "...", the variables when used in the script.







    share|improve this answer

























      up vote
      0
      down vote













      The following shellscript works for me. I have problems with json_pp, it complains about malformed JSON string, neither array, object, number, string or atom.



      #!/bin/bash

      counter=0
      while read line;
      do
      # wget "$line" -O "${line##*/}$((counter++))"
      page=$(curl -s "$line")
      echo "$page" > "loot_$((counter++))"
      done < links.txt



      • I prefer lower case variables (to decrease the risk of conflict with already existing environment variables).


      • I redirect from the input file (and avoid calling cat)


      • I update counter inside the loop


      • I use local variables loot_... for testing, but you can keep the subdirectory in the path if you wish.


      • Please notice that I have quoted, "...", the variables when used in the script.







      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        The following shellscript works for me. I have problems with json_pp, it complains about malformed JSON string, neither array, object, number, string or atom.



        #!/bin/bash

        counter=0
        while read line;
        do
        # wget "$line" -O "${line##*/}$((counter++))"
        page=$(curl -s "$line")
        echo "$page" > "loot_$((counter++))"
        done < links.txt



        • I prefer lower case variables (to decrease the risk of conflict with already existing environment variables).


        • I redirect from the input file (and avoid calling cat)


        • I update counter inside the loop


        • I use local variables loot_... for testing, but you can keep the subdirectory in the path if you wish.


        • Please notice that I have quoted, "...", the variables when used in the script.







        share|improve this answer












        The following shellscript works for me. I have problems with json_pp, it complains about malformed JSON string, neither array, object, number, string or atom.



        #!/bin/bash

        counter=0
        while read line;
        do
        # wget "$line" -O "${line##*/}$((counter++))"
        page=$(curl -s "$line")
        echo "$page" > "loot_$((counter++))"
        done < links.txt



        • I prefer lower case variables (to decrease the risk of conflict with already existing environment variables).


        • I redirect from the input file (and avoid calling cat)


        • I update counter inside the loop


        • I use local variables loot_... for testing, but you can keep the subdirectory in the path if you wish.


        • Please notice that I have quoted, "...", the variables when used in the script.








        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 28 at 19:43









        sudodus

        21.9k32871




        21.9k32871






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Ask Ubuntu!


            • 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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2faskubuntu.com%2fquestions%2f1096895%2fmy-bash-script-is-not-making-new-files%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

            How did Captain America manage to do this?

            迪纳利

            南乌拉尔铁路局